Sockets won't work in network

13.6k views Asked by At

I am working on an application that sends files via the network.

I used 2 classes to send and to receive the file that I selected.

The problem that I have faced, when I am working on localhost, is that the process goes correctly, but when I change the IP address to the network IP, it does not work.

Here is the two classes that I am using.

Class Server :

    package Test;

    import java.io.BufferedInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;

    public class Server{
 public static void main (String [] args ) throws IOException {
// create socket
ServerSocket servsock = new ServerSocket(41111);
while (true) {
  System.out.println("Waiting...");

  Socket sock = servsock.accept();
  System.out.println("Accepted connection : " + sock);

  // sendfile
  File myFile = new File ("C:\\Users\\Marrah.Zakaria\\Desktop\\test\\test.txt");
  byte [] mybytearray  = new byte [(int)myFile.length()];
  FileInputStream fis = new FileInputStream(myFile);
  BufferedInputStream bis = new BufferedInputStream(fis);
  bis.read(mybytearray,0,mybytearray.length);
  OutputStream os = sock.getOutputStream();
  System.out.println("Sending...");
  os.write(mybytearray,0,mybytearray.length);
  os.flush();
  sock.close();
  }
}
}

Class Client:

 package Test;

 import java.io.BufferedOutputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.Socket;


 public class Client{
 public static void main (String [] args ) throws IOException {
 int filesize=6022386; // filesize temporary hardcoded

 long start = System.currentTimeMillis();
 int bytesRead;
 int current = 0;

Socket sock = new Socket("192.168.1.100",41111);
System.out.println("Connecting...");

 // receive file
 byte [] mybytearray  = new byte [filesize];
 InputStream is = sock.getInputStream();
 FileOutputStream fos = new FileOutputStream("C:\\Test\\test-copy.txt");
 BufferedOutputStream bos = new BufferedOutputStream(fos);
 bytesRead = is.read(mybytearray,0,mybytearray.length);
 current = bytesRead;


do {
   bytesRead =
      is.read(mybytearray, current, (mybytearray.length-current));
   if(bytesRead >= 0) current += bytesRead;
} while(bytesRead > -1);

bos.write(mybytearray, 0 , current);
bos.flush();
long end = System.currentTimeMillis();
System.out.println(end-start);
bos.close();
sock.close();
sock.getPort();
}
}

after running an exception shows up :

Exception in thread "main" 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:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:208)

Please would you tell me what shall i do to get rid of it.


I dis-activated the receivers firewall a different exception occurred :

 Exception in thread "main" java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:69)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:208)
3

There are 3 answers

4
mkhelif On

Have you check if there is any firewall blocking the custom port (41111) on your network (also check Windows firewall)?

This is the first thing to check when you have a timeout.

1
Brian Agnew On

If you start up the server, can you telnet to this combination ?

telnet 192.168.1.100 41111

That'll tell you immediately if you have a routing issue (telnet will refuse to connect)

0
Rohini Kumar On

also try to ping 192.168.1.100 from the machine where you are running the client (i.e from the command prompt if you are in windows box)