How to tell connection timeout and socket timeout from exception trace stack? - java

I used apache http client lib in my program. I set both connection timeout and socket timeout for network calls. My understanding is that, if there is connection timeout, the client didn't reach the backend server; if there is socket timeout, the client reached the backend server but the server was not responding or very slow.
I log the following exception trace stack. How can I tell which timeout (connection or socket) caused the exception? Thanks!
java.net.SocketException: The operation timed out
at org.apache.harmony.luni.platform.OSNetworkSystem.connectStreamWithTimeoutSocketImpl(NativeMethod)
at org.apache.harmony.luni.platform.OSNetworkSystem.connectStreamWithTimeoutSocket(OSNetworkSystem.java:130)
at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:247)
at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:534)
at java.net.Socket.connect(Socket.java:1056)
at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:143)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:348)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)

From the stack trace, you can tell that it timed out while attempting to establish the connection, not while trying to read from the Socket's InputStream.

Related

What JSchExceptions are thrown when connection timeout occurs and when file transfer using put is interrupted?

I am trying to add retry mechanism to my SFTP client which uses JSch. I want to retry sending the file in the following scenarios:
Connection timeout occurs
File transfer to remote host using put is interrupted, because of connection issues
What specific JSchException messages should I check?
These are the few exceptions I came across (I am not aware of the difference):
com.jcraft.jsch.JSchException: timeout: socket is not established
com.jcraft.jsch.JSchException: Session.connect: java.net.SocketTimeoutException: Read timed out

Socket.getInputStream.read(...) throws SocketException on Android when server sends FIN packet

I'm trying to implement a client application for Android that connects to a server using Socket, exchanges some data, and then terminates the connection in a graceful way, as described in this article:
https://docs.oracle.com/javase/8/docs/technotes/guides/net/articles/connection_release.html
However, the behavior of the client socket during disconnection does not behave as expected.
We have established a connection between Android client and server.
Client finishes sending all data and calls socket.shutdownOutput() (sends FIN to server).
Server receives the last bytes and calls its socket.shutdownOutput() (sends FIN to client).
Client reads the last bytes from the server and the last call to socket.getInputStream().read(...) throws the following exception:
java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer) with cause: android.system.ErrnoException: recvfrom failed: ECONNRESET (Connection reset by peer)
I expect the last call to socket.getInputStream().read(...) should return -1, which is the behavior I get when I run the same code on OpenJDK 11 on Ubuntu.
Is there some setting of Socket which could fix the behavior on Android?

Resetting websocket

I'm writing program that communicates through web sockets and sends serialized objects. I have problem in situation, when connection between server and client is lost. I get this exception:
java.net.SocketException: Connection reset by peer: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
at java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputStream.java:1877)
at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(ObjectOutputStream.java:1786)
at java.io.ObjectOutputStream.writeNonProxyDesc(ObjectOutputStream.java:1286)
at java.io.ObjectOutputStream.writeClassDesc(ObjectOutputStream.java:1231)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1427)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
at java.io.ObjectOutputStream.writeFatalException(ObjectOutputStream.java:1577)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:351)
at common.WebSocketServer.sendResponse(WebSocketServer.java:103)
at common.WebSocketServer.listen(WebSocketServer.java:86)
at common.WebSocketServer.main(WebSocketServer.java:50)
When I use method socket.isConnected() to check, if server should abandon this client, it returns true (why?), even after closing socket and opening again it is still opened. What should I do?
There are several possible causes but the most common are that you had written to a connection that had already been closed by the peer, or that the peer closed the connection while it still had pending data to read. Both being application protocol errors.
Socket.isConnected() tells you about the state of the socket. Not of the connection.

Android app connection exception to NIO server socket

I am trying to trace a request originating from android and send some data to serversocket at each hop. I am using below code to execute client and send data to ServerSocket at each hop (tomcats having servlet projects) and they are able to send and connect.
However when I try to use same below code in android app running on emulator on same machine. It throws me timeout error.
InetSocketAddress hostAddress = new InetSocketAddress(hostAdress,
SocketServerPORT);
SocketChannel client = SocketChannel.open(hostAddress);
after open execution gives me
java.net.ConnectException: failed to connect to /192.168.0.102 (port 8090): connect failed: ETIMEDOUT (Connection timed out)
01-24 13:25:58.140 3382-3424/app W/System.err: at libcore.io.IoBridge.connect(IoBridge.java:124)
01-24 13:25:58.140 3382-3424/app W/System.err: at libcore.io.IoBridge.connect(IoBridge.java:110)
01-24 13:25:58.140 3382-3424/app W/System.err: .....
what could possibly be wrong here?
my app has
<uses-permission android:name="android.permission.INTERNET" />.
I have also used
Socket socket = new Socket(hostaddress,port)
It throws same exception.

What is the difference between java.net.SocketException: Connection reset and java.net.SocketException: Broken Pipe?

What is the difference between java.net.SocketException: Connection reset and java.net.SocketException: Broken Pipe?
I am trying to figure what are the reasons for these two exceptions. We are getting following error on our server, which is basically a soap based webservice. When I try to abort the client call the exception I am seeing is Broken pipe...
Following is the stack trace we, any help is appreciated!
2011-01-10 00:44:33,828 96893947 INFO [STDOUT] (http-0.0.0.0-8180-Processor25:) ERROR: ''
2011-01-10 00:44:33,829 96893948 INFO [STDOUT] (http-0.0.0.0-8180-Processor25:) Jan 10, 2011 12:44:33 AM com.sun.xml.rpc.server.http.JAXRPCS
ervletDelegate doGetDefault
SEVERE: JAXRPCSERVLET34: transformation failed : ClientAbortException: java.net.SocketException: Connection reset
JAXRPCSERVLET34: transformation failed : ClientAbortException: java.net.SocketException: Connection reset
at com.sun.xml.rpc.server.http.WSDLPublisher.handle(WSDLPublisher.java:109)
at com.sun.xml.rpc.server.http.JAXRPCServletDelegate.doGetDefault(JAXRPCServletDelegate.java:185)
at com.sun.xml.rpc.server.http.JAXRPCServletDelegate.doGet(JAXRPCServletDelegate.java:153)
at com.sun.xml.rpc.server.http.JAXRPCServlet.doGet(JAXRPCServlet.java:111)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
--
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)
2011-01-10 00:44:33,829 96893948 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/soa].[UserService]] (http-0.0.0.0-81
80-Processor25:) Servlet.service() for servlet UserService threw exception
javax.servlet.ServletException: JAXRPCSERVLET34: transformation failed : ClientAbortException: java.net.SocketException: Connection reset
at com.sun.xml.rpc.server.http.JAXRPCServletDelegate.doGetDefault(JAXRPCServletDelegate.java:347)
at com.sun.xml.rpc.server.http.JAXRPCServletDelegate.doGet(JAXRPCServletDelegate.java:153)
at com.sun.xml.rpc.server.http.JAXRPCServlet.doGet(JAXRPCServlet.java:111)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
'Connection reset' can occur when reading or writing. 'Broken pipe' can only occur when writing. Both are caused by writing to a connection that has already been closed by the other end, or that has been reset for some other reason.
Both Connection reset and Broken pipe occurs when the connection has been closed by the peer (i.e. application holding the connection at the other side).
Connection reset can occur when writing (see java.net.SocketOutputStream) or reading (see java.net.SocketInputStream).
Broken pipe occurs in a Native method of java.net.SocketException:
java.net.SocketException: Broken pipe
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
Thus, Broken pipe occurs at a lower communication level, as Michael Borgwardt suggested.
In most cases, I see this error when sending a big PDF to the client browser and the user kills the browser before getting the whole document (in this case, I simply ignore the error since this was the user choice to close its browser and there is nothing to correct). But it could be other reasons (e.g. EJP suggests more reason related to data communication protocols).
These are error conditions on the TCP protocol level. Both of them basically mean that the other side closed the TCP connection. The difference is in what stage of communication that happens.
Both are seemingly pointing to similar case - remote socket is no longer available for write.
Recently with my experiment, I found that Broken pipe occurs when my serve is on Unix env and I terminate the client.
015-06-26 10:53:51,028-0400 [ERROR][WS-ASync] (Handler.java:1168) Exception while writing ClientAbortException: java.net.SocketException: Broken pipe
ClientAbortException: java.net.SocketException: Broken pipe
at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:413)
at org.apache.tomcat.util.buf.ByteChunk.append(ByteChunk.java:371)
at org.apache.catalina.connector.OutputBuffer.writeBytes(OutputBuffer.java:438)
Whereas, when sever runs on windows, I see the connection reset exception
2015-06-26 09:11:31,491 ERROR [WS-ASync] (Handler.java:1168) - Exception while writing ClientAbortException: java.net.SocketException: Connection reset by peer: socket write error
ClientAbortException: java.net.SocketException: Connection reset by peer: socket write error
at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:388)
at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(ByteChunk.java:462)
at org.apache.tomcat.util.buf.ByteChunk.append(ByteChunk.java:366)
at org.apache.catalina.connector.OutputBuffer.writeBytes(OutputBuffer.java:413)
at org.apache.tomcat.util.buf.ByteChunk.append(ByteChunk.java:366)

Categories

Resources