I have a little server.jar, which listen to GET and END command on port 10000.
My client code is :
package communication;
import java.io.*;
import java.net.*;
public class Client {
public static void main(String args[]) throws Exception {
try {
Socket socket = null;
PrintWriter out = null;
BufferedReader in = null;
socket = new Socket("localhost",10000);
System.out.println("SOCKET = " + socket);
System.out.print(socket.getInetAddress() + "\n");
System.out.print(socket.getInputStream() + "\n");
System.out.println(socket.isConnected() + "\n");
out = new PrintWriter(socket.getOutputStream(),true);
in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
String str = "GET";
out.println(str);
String reponse = in.readLine();
System.out.println(socket.isConnected() + "\n");
for(int i = 0; i < 10; i++){
out.println(str); // envoi d'un message
reponse = in.readLine(); // lecture de la reponse
System.out.println("Forme recue: " + reponse);
}
System.out.println("END"); // message de terminaison
out.println("END") ;
in.close();
out.close();
socket.close();
}
catch(IOException e) {
System.out.println(e.getCause());
}
}
}
I know this code works, because it runs on one of my computers. However, I can’t make it run on another one. Configurations on both are : Windows 7 64, JRE 6, Eclipse.
My server.jar application opens a little GUi that lets me know wether the communication is open or not, which is never the case on the computer that btw get stuck on the readLine() line.
I tried to turn off Windows firewall, antivirus… nothing worked.
Does anyone know what is going wrong here?
thanks!!
Im staring at the
localhoststring, and have a hunch that could be the problem.In an answer from this post, there could be something with the
:: localhostthat needs to be commented, and also hardcoding the localhost as 127.0.0.1 in your C:\Windows\System32\drivers\etc\hosts file.By hardcoding the
localhostwith a specific IP address, the server will listen to the samelocalhostas the one your client program tries to connect to.