Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket time out after two minutes although set to more than two mintues

Tags:

java

sockets

following this post, I have the same problem and I managed to reproduce it with a simple test cast. I hope that you will be able to help me.

Let me explain, I am sending messages using sockets. Everything is working great as long as I set the so_timeout to be less than two minutes. But if I set it to be more than two minutes the socket is timed out after two minutes. So, if I set the so_timeout to be 10 seconds the socket will be timed out after 10 seconds, but if I set it to be 180 seconds the socket will be timed out after 120 seconds.

Here is a test case:

import java.io.*;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;

/**
 *
 */
public class TestSocket1 {

public static void main(String[] args) throws IOException {

    ServerSocket serverSocket = new ServerSocket();

    serverSocket.setReuseAddress(true);
    serverSocket.bind(new InetSocketAddress(1111), 0);
    serverSocket.setSoTimeout(1000);
    Socket socket = null;

    boolean send = true;

    while (send) {
        try {
            socket = serverSocket.accept();
            Thread.sleep(100);
            String messageReceived = readFromSocket(socket);

            System.out.println(messageReceived);

            if (send) {
                send = false;

                Thread.sleep(150000); // Causing 2.5 minutes delay
                // Sending message

                BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8"));
                PrintWriter printWriter = new PrintWriter(wr, true);

                String output = "Hello Back";

                printWriter.println(output);
                printWriter.flush();
                socket.shutdownOutput();

            }

        }
        catch (SocketTimeoutException ie) {
        }
        catch (Exception e) {
        }
        finally {
            if (socket != null) {
                socket.close();
            }
        }
    }


}

protected static String readFromSocket(Socket socket) throws IOException {
    StringBuilder messageReceived = new StringBuilder();
    BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));

    String line = br.readLine();

    messageReceived.append(line);

    socket.shutdownInput();

    return messageReceived.toString();
}


}

import java.io.*;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;

/**
 *
 */
public class TestSocket2 {

public static void main(String[] args) throws IOException {

    Socket socket = new Socket();
    socket.setKeepAlive(true);
    socket.setReuseAddress(true);
    socket.setTcpNoDelay(true);
    socket.setSoTimeout(180000); // Should wait 3 minutes before throwing time out exception - Actually throwing after 2 minutes

    SocketAddress socketAddress = new InetSocketAddress(1111);

    socket.connect(socketAddress, 5000);

    // Sending message

    BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8"));
    PrintWriter printWriter = new PrintWriter(wr, true);

    String output = "Hello There";

    printWriter.println(output);
    printWriter.flush();
    socket.shutdownOutput();

    String messageReceived = readFromSocket(socket);

    System.out.println(messageReceived);

}

protected static String readFromSocket(Socket socket) throws IOException {
    StringBuilder messageReceived = new StringBuilder();
    BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));

    String line = br.readLine();

    messageReceived.append(line);

    socket.shutdownInput();

    return messageReceived.toString();
}


}

You should run the TestSocket1 class first and then TestSocket2.

I am struggling with this problem for a long time and any help will be appreciated. Thank you.

  • EDIT

So I removed the dependency on SO_TimeOut and took @Nick suggestion in this post to check the available input stream before reading it. But now the problem is that after two minutes the available bytes always return 0 although the input was written to the stream. So I still have the same problem.

like image 280
Rotem Avatar asked Jun 09 '13 07:06

Rotem


People also ask

What causes socket timeouts?

Socket timeouts can occur when attempting to connect to a remote server, or during communication, especially long-lived ones. They can be caused by any connectivity problem on the network, such as: A network partition preventing the two machines from communicating. The remote machine crashing.

How do I fix SocketTimeoutException read timed out?

A possible solution for this problem within the Tomcat web application is to modify the CONTEXT. XML file, and modify the CONNECTOR definition that governs the workstation browser connectivity to the Tomcat server. Specifically, modify the connectionTimeout value. Increase this value to suppress the error condition.

How do I change my socket timeout?

Click Administration > Settings > Platform > Search > Socket Timeout (search). Change the value as required. The default value is 5000.


1 Answers

You can't increase the connect timeout beyond the platform default, which is about a minute. You can only decrease it. Trying to increase it doesn't actually make sense. What are you trying to accomplish?

like image 74
user207421 Avatar answered Sep 28 '22 09:09

user207421