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()
{
}
public void Init()
{
}
public void lisn()
{
}
public static void main(String args[])
{
}
}
//以下为会话主程序
应该特别注意,如果客户端先关闭,会话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)
{
}
public void run()
{
}
}
//以下为客户端主程序
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()
{
}
public void clientInit()
{
}
public static void main(String args[])
{
}
}