Tuesday, October 13, 2015

java.net.SocketTimeoutException: Read timed out


java.net.SocketTimeoutException: Read timed out

Dear reader,
During the development, testing, production we usually come across variout network timeout issues. My ways are as
below: I have given a Real example first then technical meaning. Code you can get in google, that I have not given here.

1) java.net.SocketTimeoutException: Read timed out
2) java.net.ConnectException: Connection refused: connect
3) java.net.SocketTimeoutException: Accept timed out
4) java.net.ConnectException: Connection timed out

Details:
1) java.net.SocketTimeoutException: Read timed out
   Real Example: I was free for 180 Seconds in evening, called my wife, she is angry from today morning as we had fought. 
   However she picked the phone, which signals that connection is established. I started telling some bla-bla and finally 
   of-course sorry for 2 Minutes expecting some response from her. But she didn't utter a single word. I waited another 
   1 Minute, then disconnected thinking she is not interested to talk :-(. This is called "Read Timed Out".
   
   Technical: When a Client/Server is trying to connect to Server/Client, Connection established, Client/Server sends some data
   to recipient and expecting some response within Sender's timeout duration, the opposite party didn't send any piece of data, Client faces
   Read time out. This means that a program was trying to receive data from a remote computer over the network, but no data was received for 
   a period of time, so the program stopped waiting for the data to be received. JVM displays below message:
   
   java.net.SocketTimeoutException: Read timed out
        at java.net.SocketInputStream.socketRead0(Native Method)
        at java.net.SocketInputStream.read(SocketInputStream.java:129)
        at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
        at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
        at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
        at java.io.InputStreamReader.read(InputStreamReader.java:167)
        at java.io.BufferedReader.fill(BufferedReader.java:136)
        at java.io.BufferedReader.readLine(BufferedReader.java:299)
        at java.io.BufferedReader.readLine(BufferedReader.java:362)


2) java.net.ConnectException: Connection refused: connect
   Real Example: I had bought a Micromax mobile 2 months back. Mobile had some problem but still under warranty. Called 
   Customer care. Got a revert while calling: "The number you dialed does not exist". This is called "Connection refused".
   
   Technical: When a client is trying to connect to a IP and Port, but there is no server with that IP and Port number or
   IP is valid but Server is not listening to that Port, Connection is refused. JVM displays below message:
   
   java.net.ConnectException: Connection refused: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
    at java.net.Socket.connect(Socket.java:529)
    at java.net.Socket.connect(Socket.java:478)
    at java.net.Socket.<init>(Socket.java:375)
    at java.net.Socket.<init>(Socket.java:189)
    at com.dmodi.sockets.SocketReadTimeout$SimpleClient.run(SocketReadTimeout.java:60)
    at java.lang.Thread.run(Thread.java:662)

    
3) java.net.SocketTimeoutException: Accept timed out
   Real Example: I had a job interview scheduled in "Wibmo Inc" at 10:30 morning. HR told another 5 minutes timeout/delay is allowed means till
   "10:35 morning". As usual I was not able to reach till "10:35 morning", "Wibmo Inc" has closed the door for me. This is 
   called "Accept timed out". 

   Technical: When Server is listening with timeout of 5000 Milliseconds, and if no client connects Server during 5000 ms, server closes
   the connection. JVM displays below message:

   java.net.SocketTimeoutException: Accept timed out
    at java.net.PlainSocketImpl.socketAccept(Native Method)
    at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:408)
    at java.net.ServerSocket.implAccept(ServerSocket.java:462)
    at java.net.ServerSocket.accept(ServerSocket.java:430)
    at com.dmodi.sockets.SocketReadTimeout$SimpleServer.run(SocketReadTimeout.java:29)
    at java.lang.Thread.run(Thread.java:662)

4) java.net.ConnectException: Connection timed out
   Real Example: I had 60 Minutes free time. I want to get some Loan from SBI, I know the manager. Went to meet him in Bank, can clearly see
   him talking to some other people in chamber. He didn't give time for me, looks not interested to give loan to me :-(. After 60 minutes of waiting 
   I left with sad face, couldn't connect him. This is called "java.net.ConnectException: Connection timed out".

   Technical: When Server IP-Port is correct and Client is trying to connect Server. Either Server is too busy or Max connections has reached or
   Firewall Rules between Client-Server is not opened. Client will be trying to connect but couldn't make so during Client's timeout duration.
   JVM displays below message:
   
   java.net.ConnectException: Connection timed out
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
    at java.net.Socket.connect(Socket.java:516)
    at java.net.Socket.connect(Socket.java:466)
    at sun.net.NetworkClient.doConnect(NetworkClient.java:157)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:365)
   
NOTE: Remember we must set timeout before making any network communication, else timeout will be controlled by Operating System
      and this will lead to blocking of connections. The duration of this no one can tell. If we set timeout, JVM controls that
      communication, however till the timeout duration, communication blocks.

=============================Thanks========================================