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.

8
задан Matt 22 March 2011 в 17:22
поделиться