When is "java.io.IOException:Connection reset by peer" thrown?
Asked Answered
S

8

87
ERROR GServerHandler  - java.io.IOException: Connection reset by peer
java.io.IOException: Connection reset by peer
        at sun.nio.ch.FileDispatcher.read0(Native Method)
        at sun.nio.ch.SocketDispatcher.read(Unknown Source)
        at sun.nio.ch.IOUtil.readIntoNativeBuffer(Unknown Source)
        at sun.nio.ch.IOUtil.read(Unknown Source)
        at sun.nio.ch.SocketChannelImpl.read(Unknown Source)
        at org.jboss.netty.channel.socket.nio.NioWorker.read(NioWorker.java:323)
        at org.jboss.netty.channel.socket.nio.NioWorker.processSelectedKeys(NioWorker.java:282)
        at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:202)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)

This log is from a game server implemented using netty. What can cause this exception ?

Steam answered 28/12, 2011 at 15:56 Comment(3)
I guess the wizard of the coast casted a spella gainst you so every single io operation you do will fail. Provide the code which cause the exception otherwise we will not be abel to help youSofthearted
Well, the client has rejected/closed the connection. You'd need the client logs to see what was the cause.Obeah
@Softhearted since, this exception seems to be network related, I can not provide the source code. thanks for the answer (and the joke) thoughSteam
D
96

java.io.IOException: Connection reset by peer

The other side has abruptly aborted the connection in midst of a transaction. That can have many causes which are not controllable from the server side on. E.g. the enduser decided to shutdown the client or change the server abruptly while still interacting with your server, or the client program has crashed, or the enduser's internet connection went down, or the enduser's machine crashed, etc, etc.

Deepfry answered 28/12, 2011 at 16:0 Comment(2)
in my situation the reverse proxy had a time-out set really short so it killed the connection before it was done.Tassel
@Adam, can you please add your solution as an answer?Costanza
G
23

To expand on BalusC's answer, any scenario where the sender continues to write after the peer has stopped reading and closed its socket will produce this exception, as will the peer closing while it still had unread data in its own socket receive buffer. In other words, an application protocol error. For example, if you write something to the peer that the peer doesn't understand, and then it closes its socket in protest, and you then continue to write, the peer's TCP stack will issue an RST, which results in this exception and message at the sender.

Goldenrod answered 29/12, 2011 at 8:55 Comment(0)
K
3

java.io.IOException in Netty means your game server tries to send data to a client, but that client has closed connection to your server.

And that exception is not the only one! There're several others. See BadClientSilencer in Xitrum. I had to add that to prevent those errors from messing my log file.

Keos answered 30/12, 2011 at 16:5 Comment(3)
It doesn't mean only that, and it isn't confined to just Netty either.Goldenrod
I don't understand. WorM posted the stack trace with a read operation but all answers explain writing problem.Extravaganza
Link not working. This one is: BadClientSilencer. Helped me!Rodger
B
1

java.io.IOException: Connection reset by peer

In my case, the problem was with PUT requests (GET and POST were passing successfully).

Communication went through VPN tunnel and ssh connection. And there was a firewall with default restrictions on PUT requests... PUT requests haven't been passing throughout, to the server...

Problem was solved after exception was added to the firewall for my IP address.

Boatsman answered 16/1, 2019 at 12:34 Comment(0)
F
0

If this happens when using Rider, when building a Docker container. Make sure all you changes are pushed to git, delete your local repo and clone everything again to start from a fresh state. This might have to do with file permissions being messed up but this fixed it immediately for me

Felicefelicia answered 20/5, 2023 at 11:6 Comment(0)
D
-1

For me useful code witch help me was http://rox-xmlrpc.sourceforge.net/niotut/src/NioServer.java

// The remote forcibly closed the connection, cancel

// the selection key and close the channel.

    private void read(SelectionKey key) throws IOException {
            SocketChannel socketChannel = (SocketChannel) key.channel();

            // Clear out our read buffer so it's ready for new data
            this.readBuffer.clear();

            // Attempt to read off the channel
            int numRead;
            try {
                numRead = socketChannel.read(this.readBuffer);
            } catch (IOException e) {
                // The remote forcibly closed the connection, cancel
                // the selection key and close the channel.
                key.cancel();
                socketChannel.close();
                return;
            }

            if (numRead == -1) {
                // Remote entity shut the socket down cleanly. Do the
                // same from our end and cancel the channel.
                key.channel().close();
                key.cancel();
                return;
            }
...
Dispenser answered 21/2, 2015 at 11:11 Comment(2)
Doesn't answer the question in any way.Goldenrod
And cancel before or after close is redundant.Goldenrod
I
-2

There are lot of factors , first see whether server returns the result, then check between server and client.

rectify them from server side first,then check the writing condition between server and client !

server side rectify the time outs between the datalayer and server from client side rectify the time out and number of available connections !

Intelligent answered 11/9, 2015 at 13:57 Comment(3)
You could add some hint on how to "rectify" and do those "checks" to improve that answer.Mielke
Hi stackoverflow.com/users/890537/m02ph3u5 1. server side , you could increase the time out between server and data source. 2. could improve the server session time out . 3.Could improve apache time out for request to server. 4.improving Keep Alive Timeout.Intelligent
He is getting the exception while reading from the server. Your answer doesn't make sense.Goldenrod
S
-2

It can also mean that the server is completely inaccessible - I was getting this when trying to hit a server that was offline

My client was configured to connect to localhost:3000, but no server was running on that port.

Silken answered 29/10, 2020 at 18:35 Comment(1)
It can't mean that. You can't have a 'connection reset' error unless you can first have a connection. You got a connection refused.Goldenrod

© 2022 - 2025 — McMap. All rights reserved.