-1

I am attempting to transfer a file from one computer to another using TCP/IP. I have written the code in java as shown below. However, the code does not work and after using multiple catch blocks I found out that this was caused due to a 'SocketException'. can anyone tell me how to resolve this? Here is my code-

TCPClient

 import java.net.*;

import java.io.; import java.util.;

public class TCPClient {

  public static void main (String args[])
  {
     try
     {
        Socket sock = new Socket("localhost", 6839);
        InputStream is = null;
        FileOutputStream out = null;
        boolean doesntexist=false;
        String filename = "D:\\"+"Test"+".pdf";
        System.out.println("Here!");
        File newplaylist = new File(filename);
        doesntexist = newplaylist.createNewFile();

        if(!doesntexist)
        {
           newplaylist.delete();
           newplaylist.createNewFile();
        }


        byte[] mybytearray = new byte[1024];

        is = sock.getInputStream();
                                // Create a new file output stream.
        out = new FileOutputStream(newplaylist);

        int count;
        while ((count = is.read(mybytearray)) >= 0) {
           out.write(mybytearray, 0, count);
        }
        out.close();
        is.close();
        sock.close();
     }      
        catch(SocketException e)
       {
        System.out.println("Socket exception");
       }
        catch(ProtocolException e)
        {
           System.out.println("Protocol exception");
        }
        catch(IOException ds)
        {
           ;
        }
  }

}

TCPServer

 import java.util.*;
 import java.io.*;
 import java.net.*;
 import java.nio.channels.*;

 class Fileserver
 {
    public static void main(String args[]) throws IOException
   {
     ServerSocket server = new ServerSocket(6839);
     File myFile = new File("E:\\file1.pdf");
     FileInputStream file = null;
     OutputStream os = null;
     Socket sock=null;

     sock = server.accept();

     try
     {

        byte[] mybytearray = new byte[1024];
        file = new FileInputStream(myFile);
        os = sock.getOutputStream();


        int count;
        while ((count = file.read(mybytearray)) >= 0) {
           os.write(mybytearray, 0, count);

        }

        os.flush();
     }


        catch(IOException e)
        {
           System.out.println("No file");
        }
        catch(IllegalBlockingModeException ea)
        {
           System.out.println("blah!");
        }

     finally
     {
        System.out.println("hello");
        file.close();
        os.close();
        sock.close();

        System.out.println("Socket closed");
     }

  }

}

1 Answers1

1

Your code is throwing away all of the useful diagnostic information which will make figuring out what the actual problem is next to impossible. (And completely ignoring IOException like that is bordering on criminal!)

The way to resolve this problem to do the following:

  • Modify the programs to output exception messages and stack traces when an unexpected exception occurs.
  • Run the programs.
  • If / when they fail, examine the diagnostics and determine what they mean ... or add them to the Question so that we can help you.

A connection timeout generally indicates there is some kind of problem at the network level. For example, the machine you are trying to connect to has died (or doesn't exist), packets are not being properly routed, or are being dropped by a firewall.

But in this case, the problem is that you are (apparently) trying to use "localhost" to communicate to another machine. That simply won't work. The "localhost" name conventionally resolves to an address on the loopback subnet; e.g. 127.0.0.1. Loopback traffic never leaves the machine it is sent from ... so you can only use "localhost" to connect to something in this machine.

(What is probably happening is that since nothing is listening for connections to the designated port on this machine, the TCP packets to open the connection are being dropped. Eventually, the client-side TCP-IP stack times out the connection attempt and the Java socket library throws the exception you are seeing.)

The solution is to replace "localhost" with a DNS name or IP address for the computer you are trying to talk to.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • After printing the stacktrace, this is the information I get- java.net.ConnectException: Connection timed out: connect at java.net.DualStackPlainSocketImpl.connect0(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:69) java.net.ConnectException: Connection timed out: connect at java.net.DualStackPlainSocketImpl.connect0(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:69) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:337) – Wendelin The wierd Jan 18 '13 at 09:47
  • I replaced it by the IPaddress of my server in the client code. However, I am still getting the same problem. It worked once and I was able to transfer the files but since then it's been timing out. Thanks for helping me with this... – Wendelin The wierd Jan 18 '13 at 17:41
  • In that case, you should be looking at the network configurations. This is not really a programming problem. – Stephen C Jan 19 '13 at 01:17