<p>I am getting the following error trying to read from a socket. I'm doing a <code>readInt()</code> on that <code>InputStream</code>, and I am getting this error. Perusing the documentation this suggests that the client part of the connection closed the connection. In this scenario, I am the server.</p>

<p>I have access to the client log files and it is not closing the connection, and in fact its log files suggest I am closing the connection. So does anybody have an idea why this is happening? What else to check for? Does this arise when there are local resources that are perhaps reaching thresholds?</p>

<hr>

<p>I do note that I have the following line:</p>

<pre><code>socket.setSoTimeout(10000);
</code></pre>

<p>just prior to the <code>readInt()</code>. There is a reason for this (long story), but just curious, are there circumstances under which this might lead to the indicated error? I have the server running in my IDE, and I happened to leave my IDE stuck on a breakpoint, and I then noticed the exact same errors begin appearing in my own logs in my IDE.</p>

<p>Anyway, just mentioning it, hopefully not a red herring. :-(</p>

[{'answer_id': 62996, 'author': 'GEOCHET', 'author_id': 5640, 'author_profile': 'https://Stackoverflow.com/users/5640', 'pm_score': 4, 'selected': False, 'text': '<p>Whenever I have had odd issues like this, I usually sit down with a tool like <a href="http://www.wireshark.org/" rel="noreferrer">WireShark</a> and look at the raw data being passed back and forth. You might be surprised where things are being disconnected, and you are only being <em>notified</em> when you try and read.</p>\n'}, {'answer_id': 63155, 'author': 'erickson', 'author_id': 3474, 'author_profile': 'https://Stackoverflow.com/users/3474', 'pm_score': 6, 'selected': False, 'text': '<p>Connection reset simply means that a TCP RST was received. This happens when your peer receives data that it can\'t process, and there can be various reasons for that.</p>\n\n<p>The simplest is when you close the socket, and then write more data on the output stream. By closing the socket, you told your peer that you are done talking, and it can forget about your connection. When you send more data on that stream anyway, the peer rejects it with an RST to let you know it isn\'t listening.</p>\n\n<p>In other cases, an intervening firewall or even the remote host itself might "forget" about your TCP connection. This could happen if you don\'t send any data for a long time (2 hours is a common time-out), or because the peer was rebooted and lost its information about active connections. Sending data on one of these defunct connections will cause a RST too.</p>\n\n<hr>\n\n<p><em>Update in response to additional information:</em> </p>\n\n<p>Take a close look at your handling of the <code>SocketTimeoutException</code>. This exception is raised if the configured timeout is exceeded while blocked on a socket operation. The state of the socket itself is not changed when this exception is thrown, but if your exception handler closes the socket, and then tries to write to it, you\'ll be in a connection reset condition. <code>setSoTimeout()</code> is meant to give you a clean way to break out of a <code>read()</code> operation that might otherwise block forever, without doing dirty things like closing the socket from another thread.</p>\n'}, {'answer_id': 4300803, 'author': 'user207421', 'author_id': 207421, 'author_profile': 'https://Stackoverflow.com/users/207421', 'pm_score': 7, 'selected': False, 'text': "<p>There are several possible causes.</p>\n\n<ol>\n<li><p>The other end has deliberately reset the connection, in a way which I will not document here. It is rare, and generally incorrect, for application software to do this, but it is not unknown for commercial software.</p></li>\n<li><p>More commonly, it is caused by writing to a connection that the other end has already closed normally. In other words an application protocol error.</p></li>\n<li><p>It can also be caused by closing a socket when there is unread data in the socket receive buffer.</p></li>\n<li><p>In Windows, 'software caused connection abort', which is not the same as 'connection reset', is caused by network problems sending from your end. There's a Microsoft knowledge base article about this.</p></li>\n</ol>\n"}, {'answer_id': 5452194, 'author': 'kml_ckr', 'author_id': 459904, 'author_profile': 'https://Stackoverflow.com/users/459904', 'pm_score': 3, 'selected': False, 'text': '<p>I had the same error. I found the solution for problem now. The problem was client program was finishing before server read the streams.</p>\n'}, {'answer_id': 17816678, 'author': 'Scott S', 'author_id': 312921, 'author_profile': 'https://Stackoverflow.com/users/312921', 'pm_score': 4, 'selected': False, 'text': '<p>Embarrassing to say it, but when I had this problem, it was simply a mistake that I was closing the connection before I read all the data.  In cases with small strings being returned, it worked, but that was probably due to the whole response was buffered, before I closed it.</p>\n\n<p>In cases of longer amounts of text being returned, the exception was thrown, since more then a buffer was coming back.</p>\n\n<p>You might check for this oversight.  Remember opening a URL is like a file, be sure to close it (release the connection) once it has been fully read.</p>\n'}, {'answer_id': 24681365, 'author': 'pmartin8', 'author_id': 1400157, 'author_profile': 'https://Stackoverflow.com/users/1400157', 'pm_score': 1, 'selected': False, 'text': "<p>I also had this problem with a Java program trying to send a command on a server via SSH. The problem was with the machine executing the Java code. It didn't have the permission to connect to the remote server. The write() method was doing alright, but the read() method was throwing a java.net.SocketException: Connection reset. I fixed this problem with adding the client SSH key to the remote server known keys. </p>\n"}, {'answer_id': 30123818, 'author': 'Pino', 'author_id': 685806, 'author_profile': 'https://Stackoverflow.com/users/685806', 'pm_score': 3, 'selected': False, 'text': "<p>I had this problem with a SOA system written in Java.  I was running both the client and the server on different physical machines and they worked fine for a long time, then those nasty connection resets appeared in the client log and there wasn't anything strange in the server log.  Restarting both client and server didn't solve the problem.  Finally we discovered that the heap on the server side was rather full so we increased the memory available to the JVM: problem solved!  Note that there was no OutOfMemoryError in the log: memory was just scarce, not exhausted.</p>\n"}, {'answer_id': 31741436, 'author': 'Davut Gürbüz', 'author_id': 413032, 'author_profile': 'https://Stackoverflow.com/users/413032', 'pm_score': 4, 'selected': False, 'text': '<p>You should inspect full trace very carefully,</p>\n<p>I\'ve a server socket application and fixed a <code>java.net.SocketException: Connection reset</code> case.</p>\n<p>In my case it happens while reading from a clientSocket <code>Socket</code> object which is closed its connection because of some reason. (Network lost,firewall or application crash or intended close)</p>\n<p>Actually I was re-establishing connection when I got an error while reading from this Socket object.</p>\n<pre><code>Socket clientSocket = ServerSocket.accept();\nis = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));\nint readed = is.read(); // WHERE ERROR STARTS !!!\n</code></pre>\n<p>The interesting thing is <code>for my JAVA Socket</code> if a client connects to my <code>ServerSocket</code> and close its connection without sending anything <code>is.read()</code> is being called repeatedly.It seems because of being in an infinite while loop for reading from this socket you try to read from a closed connection.\nIf you use something like below for read operation;</p>\n<pre><code>while(true)\n{\n  Receive();\n}\n</code></pre>\n<p>Then you get a stackTrace something like below on and on</p>\n<pre><code>java.net.SocketException: Socket is closed\n    at java.net.ServerSocket.accept(ServerSocket.java:494)\n</code></pre>\n<p>What I did is just closing ServerSocket and renewing my connection and waiting for further incoming client connections</p>\n<pre><code>String Receive() throws Exception\n{\ntry {                   \n            int readed = is.read();\n           ....\n}catch(Exception e)\n{\n        tryReConnect();\n        logit(); //etc\n}\n\n\n//...\n}\n</code></pre>\n<p>This reestablises my connection for unknown client socket losts</p>\n<pre><code>private void tryReConnect()\n        {\n            try\n            {\n                ServerSocket.close();\n                //empty my old lost connection and let it get by garbage col. immediately \n                clientSocket=null;\n                System.gc();\n                //Wait a new client Socket connection and address this to my local variable\n                clientSocket= ServerSocket.accept(); // Waiting for another Connection\n                System.out.println(&quot;Connection established...&quot;);\n            }catch (Exception e) {\n                String message=&quot;ReConnect not successful &quot;+e.getMessage();\n                logit();//etc...\n            }\n        }\n</code></pre>\n<p>I couldn\'t find another way because as you see from below image you can\'t understand whether connection is lost or not without a <code>try and catch</code> ,because everything seems right . I got this snapshot while I was getting <code>Connection reset</code> continuously.</p>\n<p><a href="https://i.stack.imgur.com/T75Ul.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/T75Ul.png" alt="enter image description here" /></a></p>\n'}, {'answer_id': 57515659, 'author': 'Sumiya', 'author_id': 2473061, 'author_profile': 'https://Stackoverflow.com/users/2473061', 'pm_score': 2, 'selected': False, 'text': '<p>Check your server\'s Java version. Happened to me because my Weblogic 10.3.6 was on JDK 1.7.0_75 which was on TLSv1. The rest endpoint I was trying to consume was shutting down anything below TLSv1.2. </p>\n\n<p>By default Weblogic was trying to negotiate the strongest shared protocol. See details here: <a href="https://stackoverflow.com/questions/34283829/issues-with-setting-https-protocols-system-property-for-https-connections">Issues with setting https.protocols System Property for HTTPS connections</a>.</p>\n\n<p>I added verbose SSL logging to identify the supported TLS. This indicated TLSv1 was being used for the handshake.<br/>\n<code>-Djavax.net.debug=ssl:handshake:verbose:keymanager:trustmanager -Djava.security.debug=access:stack</code></p>\n\n<p>I resolved this by pushing the feature out to our JDK8-compatible product, JDK8 defaults to TLSv1.2. For those restricted to JDK7, I also successfully tested a workaround for Java 7 by upgrading to TLSv1.2. I used this answer: <a href="https://stackoverflow.com/questions/39157422/how-to-enable-tls-1-2-in-java-7">How to enable TLS 1.2 in Java 7</a></p>\n'}, {'answer_id': 62217112, 'author': 'bmck', 'author_id': 2119761, 'author_profile': 'https://Stackoverflow.com/users/2119761', 'pm_score': 0, 'selected': False, 'text': '<p>In my experience, I often encounter the following situations;</p>\n\n<ol>\n<li><p>If you work in a corporate company, contact the network and security team. Because in requests made to external services, it may be necessary to <strong>give permission for the relevant endpoint.</strong></p></li>\n<li><p>Another issue is that the <strong>SSL certificate may have expired</strong> on the server where your application is running.</p></li>\n</ol>\n'}, {'answer_id': 68480666, 'author': 'tsotzolas', 'author_id': 3832031, 'author_profile': 'https://Stackoverflow.com/users/3832031', 'pm_score': 1, 'selected': False, 'text': '<p>In my case was <code>DNS problem</code> .<br />\nI put in <code>host file</code> the resolved IP and everything works fine.\nOf course it is not a permanent solution put this give me time to fix the DNS problem.</p>\n'}, {'answer_id': 68488293, 'author': 'Thiago Ferreira', 'author_id': 8907047, 'author_profile': 'https://Stackoverflow.com/users/8907047', 'pm_score': 0, 'selected': False, 'text': '<p>I\'ve seen this problem. In my case, there was an error caused by reusing the same ClientRequest object in an specific Java class. That project was using <a href="https://docs.jboss.org/resteasy/docs/3.0.13.Final/javadocs/org/jboss/resteasy/client/ClientRequest.html" rel="nofollow noreferrer">Jboss Resteasy</a>.</p>\n<ol>\n<li>Initially only one method was using/invoking the object ClientRequest (placed as global variable in the class) to do a request in an specific URL.</li>\n<li>After that, another method was created to get data with another URL, reusing the same ClientRequest object, though.</li>\n</ol>\n<p><strong>The solution: in the same class was created another ClientRequest object and exclusively to not be reused.</strong></p>\n'}, {'answer_id': 71227340, 'author': 'Djek-Grif', 'author_id': 2471275, 'author_profile': 'https://Stackoverflow.com/users/2471275', 'pm_score': 0, 'selected': False, 'text': '<p>In my case it was problem with TSL version. I was using Retrofit with OkHttp client and after update ALB on server side I should have to delete my config with <strong>connectionSpecs</strong>:</p>\n<pre><code>OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();\n        List&lt;ConnectionSpec&gt; connectionSpecs = new ArrayList&lt;&gt;();\n        connectionSpecs.add(ConnectionSpec.COMPATIBLE_TLS);\n  //      clientBuilder.connectionSpecs(connectionSpecs);\n</code></pre>\n<p>So try to remove or add this config to use different TSL configurations.</p>\n'}, {'answer_id': 73825492, 'author': 'MaVRoSCy', 'author_id': 1387157, 'author_profile': 'https://Stackoverflow.com/users/1387157', 'pm_score': 0, 'selected': False, 'text': '<p>I used to get the \'NotifyUtil::java.net.SocketException: Connection reset  at java.net.SocketInputStream.read(SocketInputStream.java:...\' message in the Apache Console of my Netbeans7.4 setup.</p>\n<p>I tried many solutions to get away from it, what worked for me is enabling the TLS on Tomcat.</p>\n<p>Here is how to:</p>\n<blockquote>\n<p>Create a keystore file to store the server\'s private key and\nself-signed certificate by executing the following command:</p>\n<p>Windows:</p>\n<p>&quot;%JAVA_HOME%\\bin\\keytool&quot; -genkey -alias tomcat -keyalg RSA</p>\n<p>Unix:</p>\n<p>$JAVA_HOME/bin/keytool -genkey -alias tomcat -keyalg RSA</p>\n<p>and specify a password value of &quot;changeit&quot;.</p>\n</blockquote>\n<p>As per <a href="https://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html" rel="nofollow noreferrer">https://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html</a>\n(This will create a .keystore file in your localuser dir)</p>\n<p>Then edit server.xml (uncomment and edit relevant lines) file (%CATALINA_HOME%apache-tomcat-7.0.41.0_base\\conf\\server.xml)  to enable SSL and TLS protocol:</p>\n<pre><code> &lt;Connector port=&quot;8443&quot; protocol=&quot;HTTP/1.1&quot; SSLEnabled=&quot;true&quot;\n                maxThreads=&quot;150&quot; scheme=&quot;https&quot; secure=&quot;true&quot;\n                clientAuth=&quot;false&quot; sslProtocol=&quot;TLS&quot; keystorePass=&quot;changeit&quot; /&gt;\n</code></pre>\n<p>I hope this helps</p>\n'}]
['https://Stackoverflow.com/questions/62929', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']