当前位置: 代码迷 >> J2SE >> java.net.SocketException: Connection reset //这句是等了好长时间才出来。
  详细解决方案

java.net.SocketException: Connection reset //这句是等了好长时间才出来。

热度:254   发布时间:2016-04-24 18:06:53.0
关于虚拟机与XP主机SOCKET通信的奇怪问题
先代码如下:
server:
 try 
  {
  int i=1;
  ServerSocket server = new ServerSocket(listenPort);
  Socket incomingConnection = null;
  System.out.println("server is ok,wait client request. port="+listenPort);
  while(true)
  {
  System.out.println("wait... ");
  incomingConnection = server.accept(); 
  System.out.println("client request "+i);
  i++;
  handleConnection(incomingConnection);
  }
  }  
...........................

////////////////////////////////////////////////////////////////////////////////////////
client

  System.out.println("begin connection...hostIp="+hostIp+":hostport="+hostPort);
  Socket client = new Socket(hostIp,hostPort);
  // System.out.println("client.isConnected()=" + client.isConnected());
  System.out.println("begin connection......");
  socketReader = new BufferedReader(new InputStreamReader(client.getInputStream()));
  socketWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(client.getOutputStream())),true); 
  System.out.println("connetc is ok");

////////////////////////////////////////////////////////////////

  System.out.println("send message begin");
  socketWriter.println(fileNameToGet);  
  System.out.println("send message end");
  socketWriter.flush();
  System.out.println("send message flush");
  String line = null;
  System.out.println(socketReader.readLine());
  while((line=socketReader.readLine())!=null)
  System.out.println("line="+line);
  fileLines.append(line+"\n");

////////////////////////////////////////////////////////////////////////

具体情况是:上面代码同时在xp或虚拟机(centos5.5)运行,一切正常。但现在把server代码在虚拟机centos运行,client在XP上运行,显示如下:server:
server is ok,wait client request. port=25888
wait... 

client :
begin connection...hostIp=10.1.14.71:hostport=25888
client.isConnected()=true
begin connection......
connetc is ok
send message begin
send message end
send message flush
java.net.SocketException: Connection reset //这句是等了好长时间才出来。

为何client提示连接上了,server没反应?防火墙关了,而且xp能够与虚拟机互相ping通。






------解决方案--------------------
这可真的不了解,你可能要改变虚拟机的连接方式才行。
------解决方案--------------------
handleConnection

去调试吧,看看是如何返回数据的。
------解决方案--------------------
估计虚拟机运行client,xp运行server也会堵塞
很有可能是println readLine造成的
系统不一样,换行符也不一样,如果系统不能正确读取到换行符,就一直堵塞
可以尝试启动的时候,带上属性设置参数

java -Dline.separator=xxx your_java
------解决方案--------------------
可能是println readLine造成的
系统不一样,换行符也不一样,如果系统不能正确读取到换行符,就一直堵塞
可以尝试启动的
------解决方案--------------------
windows换行是\r\n,十六进制数值是:0D0A。
LINUX换行是\n,十六进制数值是:0A
centos 是LINUX的吧
------解决方案--------------------
  相关解决方案