Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting "SocketException : Connection reset by peer" in Android

My app needs to contact the same device it is working on, via http://127.0.0.1/... (a localhost url).

For some reason, about 50% of the times (and maybe exactly 50%) when I reach a website there with JSON content, I get the exception:

java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)

For the other 50%, I get perfectly good results. I've tried to do polls (and even large delay between polls), but I keep getting the same weird results.

I've searched the internet and also here, and I'm not sure why it occurs. Does the peer mean that the client has caused it? Why does it happen, and how should i handle it?

Some websites say that it's a common thing, but I didn't find what's the best thing to do in such cases.

like image 558
android developer Avatar asked Jun 26 '12 12:06

android developer


People also ask

What is causing Connection reset by peer?

The error message "Connection reset by peer" appears, if the web services client was waiting for a SOAP response from the remote web services provider and the connection was closed prematurely. One of the most common causes for this error is a firewall in the middle closing the connection.

What causes SocketException connection reset?

The java. net. SocketException: Connection reset error usually comes when one of the parties in TCP connection like client or server is trying to read/write data, but other parties abruptly close the connection like it was crashed, stopped or terminated.


2 Answers

Ok, the answer was that it's the server's fault - it had to close the connection after each request.

It might be that Android keeps a pool of connections and use the old one or something like that.

Anyway , now it works.


EDIT: according to the API of HttpURLConnection, this can be solved on the client side too:

The input and output streams returned by this class are not buffered. Most callers should wrap the returned streams with BufferedInputStream or BufferedOutputStream. Callers that do only bulk reads or writes may omit buffering. When transferring large amounts of data to or from a server, use streams to limit how much data is in memory at once. Unless you need the entire body to be in memory at once, process it as a stream (rather than storing the complete body as a single byte array or string).

To reduce latency, this class may reuse the same underlying Socket for multiple request/response pairs. As a result, HTTP connections may be held open longer than necessary. Calls to disconnect() may return the socket to a pool of connected sockets. This behavior can be disabled by setting the http.keepAlive system property to false before issuing any HTTP requests. The http.maxConnections property may be used to control how many idle connections to each server will be held.

Taken from: developer.android.com/reference/java/net/HttpURLConnection.html

like image 148
android developer Avatar answered Sep 19 '22 06:09

android developer


Try to set this property for your HttpURLConnection before connecting:

conn.setRequestProperty("connection", "close"); 

This will disable "keep-alive" property which is on by default.

like image 26
valerybodak Avatar answered Sep 19 '22 06:09

valerybodak