Getting "Socketexception:Connection Reset by Peer" in Android

Getting SocketException : Connection reset by peer in Android

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

java.net.SocketException: Connection reset by peer

Guys the error was occurring because of timeout due to slow response in the internet connection and thus the connection was getting reset as per the coding at the backend.

java.net.SocketException: Connection reset by peer: socket write error When serving a file

It is possible for the TCP socket to be "closing" and your code to not have yet been notified.

Here is a animation for the life cycle. http://tcp.cs.st-andrews.ac.uk/index.shtml?page=connection_lifecycle

Basically, the connection was closed by the client. You already have throws IOException and SocketException extends IOException. This is working just fine. You just need to properly handle IOException because it is a normal part of the api.

EDIT: The RST packet occurs when a packet is received on a socket which does not exist or was closed. There is no difference to your application. Depending on the implementation the reset state may stick and closed will never officially occur.

Getting java.net.SocketException Connection reset error in socket communication with threads

java.net.SocketException: Connection reset

This means the OS has reseted the connection because the process on the other end is no longer running. It looks like your client crashes after processing first reply from the server.

Scanner scanner = new Scanner(System.in);
scanner.close();

Do not close Scanners operating on System.in. This will close System.in and you will not be able to read anything anymore from there.



Related Topics



Leave a reply



Submit