0

在 android 文档中,以下代码出现在线程的 run() 段中:

BluetoothSocket socket = null;
        // Keep listening until exception occurs or a socket is returned
        while (true) {
            try {
                socket = mmServerSocket.accept();
            } catch (IOException e) {
                break;
            }
            // If a connection was accepted
            if (socket != null) {
                // Do work to manage the connection (in a separate thread)
                manageConnectedSocket(socket);
                mmServerSocket.close();
                break;
            }
        }

但是,accept() 方法会阻塞线程。因此,我不明白为什么需要 while() 循环,特别是因为在所有可能的情况下,while 循环在第一次运行时就被破坏了。

有任何想法吗?

4

1 回答 1

1

通常在接受和处理一个套接字后不会有中断:您将无限期地循环接受套接字。

这是一个愚蠢的例子。

于 2016-09-21T00:37:42.880 回答