2

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)
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Zakaria Marrah
  • 755
  • 6
  • 15
  • 28
  • This is probably a network issue. Can you ping the machines where this is not working (from each other)? – Bill the Lizard Oct 09 '12 at 14:21
  • When switching from localhost to over network and encountering issues, I normally try to verify the two hosts can communicate with each other. Try pinging the hosts from each other first. – Lipongo Oct 09 '12 at 14:21
  • I did that already, it appears that my machine recognize the receiver machine. – Zakaria Marrah Oct 09 '12 at 14:26
  • Were you able to solve the issue? If yes please do share it with me because i am facing similar problem. – Nitesh Verma Aug 01 '13 at 06:41

3 Answers3

2

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)

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
1

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.

mkhelif
  • 1,551
  • 10
  • 18
  • I dis-activated it but the same exception occurs. – Zakaria Marrah Oct 09 '12 at 14:30
  • @ZakariaMarrah Once the firewall is deactivated and if you receive a connection refused, it means that the server is not listening on the port. Is the server started on the machine? Do you have any exception on server-side? – mkhelif Oct 09 '12 at 15:21
  • No, my server is started. What i did is that i turned the server's firewall off and then the connectionrefused Exception showed up. – Zakaria Marrah Oct 09 '12 at 15:36
  • Is there any other suggestion other then sockets to send a file via network ?? – Zakaria Marrah Oct 09 '12 at 15:40
1

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)

Rohini Kumar
  • 249
  • 1
  • 5
  • 15