44

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • Read this for the complete explanation and possible resolution: http://stackoverflow.com/questions/30538640/javax-net-ssl-sslexception-read-error-ssl-0x9524b800-i-o-error-during-system?lq=1 – Devendra Vaja Jul 14 '15 at 18:20
  • 1
    See also http://stackoverflow.com/questions/1434451/what-does-connection-reset-by-peer-mean – Raedwald Jan 11 '16 at 12:47

6 Answers6

22

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

buczek
  • 2,011
  • 7
  • 29
  • 40
android developer
  • 114,585
  • 152
  • 739
  • 1,270
7

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.

valerybodak
  • 4,195
  • 2
  • 42
  • 53
3

This is an old thread i know. But this might help someone.

In my case this error was caused by the .NET WCF (soap) service. One of the objects in the returning result had a DataMember with get{} property but no set{} property.

For serialization to occur every DataMember should have both get{} & set{} available. I implemented an empty set{} (empty due to my business rules), and problem was solved.

My scenerio is a specific bad server implementation, but maybe it'll help someone saving time when troubleshooting.

Cucumen
  • 87
  • 7
  • 2
    @androiddeveloper : The Android OS is the client side, but the server side could be anything. So his answer may not answer the OP, but it could answer someone else. +1 – Lionel Pire May 10 '17 at 08:36
1

I was having a lot of these Connection reset by peer when I was visiting certain web pages or downloading files (from my app or the Android browser).

Turned out it was my 3G carrier that blocked the connections (e.g. downloading an .exe file was forbidden).

Do you have the same problem on Wifi ?

Sébastien
  • 13,831
  • 10
  • 55
  • 70
  • since the communication is with the same device , i would expect 0 problems communicating with it unless it has really serious problems. i don't think it has anything to do with wifi vs 3g , but i use wifi . – android developer Jun 26 '12 at 14:56
  • I guess I misunderstood the question. By `i reach a website there with json content`, you mean that you reach a website hosted by a web server running on the phone ? – Sébastien Jun 26 '12 at 15:28
  • yes ,the device is both a client and a server , and the webpage includes only json response . – android developer Jun 26 '12 at 16:13
0

in my situation the problem has been solved by cleaning the proxy address and port from APN which was produced by the operator. as I have tested, using IP address of remote server instead of domain name also can solve the problem.

ahmad pj
  • 41
  • 1
  • 3
0

In our case the issue is in the Server side (Application Pool configuration in IIS). I resolved it by setting Maximum Worker Processes to 1. Setting it with value more than 1 will enable Web Garden which seems to be the issue.

j-j
  • 93
  • 1
  • 13