加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

java.net.SocketException: Connection reset问题

(2010-07-12 13:12:44)
标签:

it

分类: の技术

Connection Reset by Peer :

One of the issues that developers frequently run into is the Connection reset by peer exception: 

Exception in thread "main" java.net.SocketException:
Connection reset by peer: JVM_recv in socket input stream read.

This basically means that a network error occurred while the client was receiving data from the server. But what is really happening is that the server actually accepts the connection, processes the request, and sends a reply to the client. However, when the server closes the socket, the client believes that the connection has been terminated abnormally because the socket implementation sends a TCP reset segment telling the client to throw away the data and report an error. 

Sometimes, this problem is caused by not properly closing the input/output streams and the socket connection. Make sure you close the input/output streams and socket connection properly. If everything is closed properly, however, and the problem persists, you can work around it by adding Thread.sleep(1000) before closing the streams and the socket. This technique, however, is not reliable and may not work on all systems.


谢谢大家,我的问题已经解决了,问题的原因是数据库和应用服务器在不同网段有blob类型的数据操作产生的,应用服务器与数据库在统一网段就没有此问题了

 

Java API中封装了大量的函数,供编写网络通信程序时使用.
这使得java在网络方面具有强大的功能.
用java编写TCP方式的通信程序比较简单,但也有一些问题需要注意.

以下为监听主程序,监听程序在发现客户端连接后,启动一个会话socket线程,以实现实时发送,接收信息
和多客户端同时工作.
import java.io.*;
import java.lang.*;
import java.net.ServerSocket;
import java.net.Socket;
//主程序一直处于监听状态,有连接则启动一个线程进行处理,以实现多个客户端
public class listenserve
{
private ServerSocket ss;
private boolean listening=true;
public listenserve()
{
  Init();//初始化
  lisn();//启动监听 
}
public void Init()
{
  try
  {
   ss=new ServerSocket(10015,10);
  }
  catch(IOException ie)
  {
    System.out.println("无法在10015端口监听");
    ie.printStackTrace();
  }
}
public void lisn()
{
  try
  {
   while(listening)
    new Thread(new dialogserve(ss.accept())).start();
    }
   catch(IOException ie)
   {ie.printStackTrace();}
}
public static void main(String args[])
{
  new listenserve();
}
}

//以下为会话主程序
应该特别注意,如果客户端先关闭,会话socket中可能抛出socketexception:connection reset
这应该在程序中进行处理,这也是较易忽略的问题.
import java.io.*;
import java.lang.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
public class dialogserve implements Runnable
{
private Socket s;
private InputStream in;
private String rev,temp;
private byte b[];
private int len;
public dialogserve(Socket ss)
{
  s=ss;
  b=new byte[1024];
  try
  {
  in=s.getInputStream();
  }catch(IOException ie)
  {
   ie.printStackTrace();
   }
  rev="";
}
public void run()
{
  try
  {
   while(s.isConnected()==true)
   {
    if((len=in.read(b))!=-1)
    {
     temp=new String(b,0,len);
      rev+=temp;
      System.out.print(rev);
      temp=null;
      Thread.sleep(1000);
    }
   
   in.close();
   s.close();
   System.out.println("会话socket已断开!");
  }
  catch(SocketException se)
  {
   System.out.println("客户端已断开!");
    System.exit(0);
  }
  catch(IOException io)
  {
   io.printStackTrace();
   System.exit(0);
  }
  catch(InterruptedException ire)
  { ire.printStackTrace();}
}
}
//以下为客户端主程序
import java.io.*;
import java.net.Socket;
import java.lang.*;
public class client
{
private Socket con;//客户端连接socket
private OutputStream out;
private String sen;
private byte b[];
public client()
{
  clientInit();
}
public void clientInit()
{
  try
  {
   con=new Socket("localhost",10015);
   con.setSoTimeout(10000);
   b=new byte[1024];
   OutputStream out=con.getOutputStream();
   sen="hello serve,以TCP方式发送数据!";
   b=sen.getBytes();
   out.write(b);
   out.flush();
   out.close();
   con.close();
  }
  catch(IOException ie)
  {
   ie.toString();
  }
}
public static void main(String args[])
{
  new client();
}
}

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有