【发布时间】:2012-01-22 17:01:38
【问题描述】:
我有一个小 server.jar,它在端口 10000 上侦听 GET 和 END 命令。
我的客户代码是:
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());
}
}
}
我知道这段代码有效,因为它在我的一台计算机上运行。但是,我不能让它在另一个上运行。两者的配置是:Windows 7 64、JRE 6、Eclipse。
我的 server.jar 应用程序打开了一个小 GUI,让我知道通信是否打开,这在计算机上绝不会卡在 readLine() 行上。
我尝试关闭 Windows 防火墙、防病毒软件...没有任何效果。
有人知道这里出了什么问题吗?
谢谢!!
【问题讨论】:
-
我们需要更多信息来帮助您。你得到什么错误?什么行为等等。
-
我没有收到任何错误,程序只是卡在 String reponse = in.readLine();就我而言,实际上是缺乏让我感到困扰的行为;)
-
你到底想测试什么?如果您想要服务器-客户端通信,为什么没有任何 SocketServer?
-
“服务器”只是一个小的可执行 jar。我启动它,然后它在端口 10000 上侦听,等待 GET 和 END 命令。我用 Telnet 对其进行了测试,工作 #1
标签: java windows eclipse socket.io