中断连接sockets

我有一个GUI,其中包含要连接的服务器列表。 如果用户单击它连接到它的服务器。 如果用户单击第二个服务器,它将断开第一个服务器并连接到第二个服务器。 每个新连接都在一个新线程中运行,以便程序可以执行其他任务。

但是,如果用户在第一个服务器仍然连接时单击第二个服务器,则会有两个同时连接。

我正在使用它连接,而connect()是阻塞的行:

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

我想也许是Thread.currentThread().interrupt(); 会工作,但没有。

我是否需要稍微重构我的代码以便继续进行第一次连接,但是之后会立即关闭它? 或者实际上是一种中断connect方法的方法。

如果您正在使用阻塞套接字实现,则中断线程将不会“取消”或中断您的套接字连接。 打破“阻塞调用”的唯一方法是“关闭”套接字。 您可以在Runnable任务上公开一个方法(例如cancel ),该方法关闭套接字并在用户尝试连接到第二个服务器时清理资源。

如果你想要的话,你可以看看我一次性尝试中断阻塞调用的线程。

你可以改为使用非阻塞套接字吗? 我不是Java专家,但看起来SocketChannel是他们的非阻塞套接字类。

这是一个例子:

 // 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) { } 

取自此处: http : //www.exampledepot.com/egs/java.nio/NbClientSocket.html

在while循环中,您可以检查一些需要取消和挽救的共享通知,然后关闭SocketChannel。

我尝试了建议的答案,但没有任何对我有用。 所以我所做的是,不是将连接超时设置为10秒,而是尝试连续连接5次,连接超时为2秒。 我还声明了一个全局变量boolean cancelConnection

每次抛出超时exception时,我都可以根据cancelConnection的值进行中断或继续循环。

这是我正在编写的Android应用程序的代码片段:

 try { SocketAddress socketaddres = new InetSocketAddress(server.ip,server.port); int max=5; for (int i = 1; i<=max; i++) { try { socket = new Socket(); socket.connect(socketaddres, 2000); break; } catch (Exception e) { Log.d(TAG, "attempt "+i+ " failed"); if (cancelConnection) { Log.d(TAG, "cancelling connection"); throw new Exception(); } else if (i==max) { throw new Exception(); } } } } catch (Exception e) { if (cancelConnection) { // Do whatever you would do after connection was canceled. } else { // Do whatever you would do after connection error or timeout } }