在Android中如何停止正在等待新套接字的线程

发布于 2024-10-15 18:56:04 字数 496 浏览 0 评论 0原文

我正在开发一个使用 Socket 连接到服务器的软件;

connectionThread = new Thread(new Runnable( ) {
    public void run() {
        InetAddress serverAddress = InetAddress.getByName(ip);
        serverSocket = new Socket(serverAddress, port);
        //do more stuff
        }
    });
connectionThread.start();

当客户端没有连接到服务器时,线程将继续等待新套接字的返回,直到达到超时。

我想让用户取消该操作。然后,我尝试在用户单击后退按钮时调用 connectionThread.interrupt() 。但线程仍在运行。

我可以让线程运行直到新的 Socket 超时,但我认为这不是很好。

I'm developing a software that connects to a server using a Socket;

connectionThread = new Thread(new Runnable( ) {
    public void run() {
        InetAddress serverAddress = InetAddress.getByName(ip);
        serverSocket = new Socket(serverAddress, port);
        //do more stuff
        }
    });
connectionThread.start();

When the client does not connect to the server the Thread keeps waiting for the return of the new Socket until timeout is reached.

I want to enable the user to cancel that action. I tried then to call connectionThread.interrupt() when the user clicks the back button. But the thread keeps running.

I could let the thread runs until the new Socket timeout, but I think that It's not very good.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不在是我 2024-10-22 18:56:04

不要使用new Socket(serverAddress, port);。相反,首先使用 new Socket() 创建一个新套接字,然后使用 Socket.connect() 连接该套接字。这样,您可以

1) 指定连接超时(将引发 SocketTimeoutException),

2) 使用 Socket.close() 从不同线程取消进程(将引发SocketException)。

这是使用此方法的代码片段:

connectionThread = new Thread(new Runnable( ) {
    public void run() {
        try {
            InetAddress serverAddress = InetAddress.getByName(ip);
            serverSocket = new Socket();
            serverSocket.connect(new InetSocketAddress(serverAddress,port),TIMEOUTMS);
            //do more stuff
        } catch (SocketTimeoutException ste)
        {
            // connect() timeout occurred
        } catch (SocketException se)
        {
            // socket exception during connect (e.g. socket.close() called)
        }
    }});
connectionThread.start();

Don't use new Socket(serverAddress, port);. Instead, first create a new socket using new Socket(), and then connect the socket using Socket.connect(). This way, you can

1) specify a timeout for the connection (SocketTimeoutException will be raised), and

2) cancel the process from a different thread using Socket.close() (SocketException will be raised).

Here is your code snippet using this method:

connectionThread = new Thread(new Runnable( ) {
    public void run() {
        try {
            InetAddress serverAddress = InetAddress.getByName(ip);
            serverSocket = new Socket();
            serverSocket.connect(new InetSocketAddress(serverAddress,port),TIMEOUTMS);
            //do more stuff
        } catch (SocketTimeoutException ste)
        {
            // connect() timeout occurred
        } catch (SocketException se)
        {
            // socket exception during connect (e.g. socket.close() called)
        }
    }});
connectionThread.start();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文