Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interrupt a connecting socket

I have a GUI with a list of servers to connect to. If a user clicks a server it connects to it. If a user clicks a second server, it will disconnect the first and connect to the second. Each new connection runs in a new thread so that the program can perform other tasks.

However, if a user clicks a second server while the first is still connecting, there are two simultaneous connections.

I'm connecting using this, and connect() is the line that blocks:

Socket socket = new Socket();
socket.connect(socketAddress, connectTimeout);

I thought maybe Thread.currentThread().interrupt(); would work, but didn't.

Do I have to restructure my code a bit so that it continues making the first connection, but closes it straight after? Or is there actually a way to interrupt the connect method.

like image 551
Matt Avatar asked Mar 22 '11 17:03

Matt


People also ask

How do I interrupt a socket?

The only way of breaking out of the 'blocking call' is to 'close' the socket. You can expose a method on your Runnable tasks (e.g. cancel ) which close the socket and clean up the resources when the user tries connecting to a second server.

What is a SocketException?

The SocketException is an exception in Java that is thrown to indicate that an error was encountered while creating or accessing a Socket. Since the SocketException is a checked exception, it either needs to be thrown or surrounded by a try-catch block in code.


2 Answers

If you are using a blocking socket implementation, interrupting the thread won't 'cancel' or interrupt your socket connection. The only way of breaking out of the 'blocking call' is to 'close' the socket. You can expose a method on your Runnable tasks (e.g. cancel) which close the socket and clean up the resources when the user tries connecting to a second server.

If you want you can have a look at my throwaway attempt at interrupting threads which make blocking calls.

like image 154
Sanjay T. Sharma Avatar answered Sep 25 '22 03:09

Sanjay T. Sharma


Can you instead use a non-blocking socket? I'm not much of a Java expert, but it looks like SocketChannel is their non-blocking socket class.

Here is an example:

// Create a non-blocking socket and check for connections
try {
    // Create a non-blocking socket channel on port 80
    SocketChannel sChannel = createSocketChannel("hostname.com", 80);

    // Before the socket is usable, the connection must be completed
    // by calling finishConnect(), which is non-blocking
    while (!sChannel.finishConnect()) {
        // Do something else
    }
    // Socket channel is now ready to use
} catch (IOException e) {
}

Taken from here: http://www.exampledepot.com/egs/java.nio/NbClientSocket.html

Inside the while loop you can check for some shared notification that you need to be cancelled and bail out, closing the SocketChannel as you go.

like image 42
Luke Avatar answered Sep 25 '22 03:09

Luke