<< 2005年12月29日 | 首页 | 2005年12月31日 >>

What does the exception "java.net.SocketException: Connection reset by peer" mean? How can this be avoided?

It usually happens when the connection was reset by the peer -- that is, when the client unexpectedly closed the socket. This happens when the user clicks "stop", or "reload", or "back", or enters a new URL, or closes his Internet connection, while a resource is still being downloaded (before your server gets to close its socket). This is normal behavior and you shouldn't worry about it.

The server is reporting "connection reset by peer" in the middle of a request. That means one of the following things:

  • The browser started a request and the user pressed STOP.
  • The browser started an request in one frame and JavaScript on another frame told it to go somewhere else (which has the same effect as a user pressing STOP).
  • You have a meta refresh reload someplace that is firing so fast that the previous request hasn't had a chance to complete yet. The five second periodicity makes me pretty suspicious of this.
  • You have some network component in between (a router or proxy) that is misbehaving.
  • You have some completely separate connection attempts going on that are disobeying the HTTP protocol (such as doing a telnet connection to port 8080 and then disconnecting).
标签 : ,

java URLConnection and timeout and non-blocking io

自从JDK1.5以后java.net.URLConnection加入了setConnectTimeout(int timeout),但是也有办法在JDK1.5以前获得这个功能。
Adding Socket Timeout to java.net.URLConnection
HTTPClient Version 0.3-3
The Jakarta Commons HttpClient
另外,可以通过以下两个语句来设置相应的超时:
System.setProperty("sun.net.client.defaultConnectTimeout", 连接超时毫秒数);
System.setProperty("sun.net.client.defaultReadTimeout", 读数据超时毫秒数);
参见:http://java.sun.com/j2se/1.4.2/docs/guide/net/properties.html
google:set Timeout on URLConnection

标签 :

Non-blocking IO and blocking queues

Well performing NIO code is a Good Thing. But getting details of how to do it right is difficult. The examples supplied with the JDK are complete noddy. There's a link on Sun's site to an example but that has a race condition. O'Reilly's NIO book cops out on how to use Selector properly. The thing that could make that book useful.

阅读全文……

标签 :