Android socket.close() 导致“系统调用被取消”?

发布于 2024-12-28 16:27:58 字数 1622 浏览 3 评论 0原文

我的 MainActivity 启动另一个名为 UdpListener 的线程。在 onResume 中,线程将被启动(这是可行的),在 onPause 中它应该停止,但是 socket.close() 会在 LogCat 中导致错误消息“系统调用已取消”。 UdpListener 本身在 JUnit 测试中工作正常,因此这一定是某种 Android 问题。

这些是我的线程对应的方法:

private void setThreadRunning(boolean running) throws MyOwnException {
    this.running = running;

    if (running) {
        try {
            serverSocket = new DatagramSocket(9800);
        } 
        catch (SocketException e) {
            exceptionThrown = true;
            this.running = false;
            Log.e(TAG, e.getMessage());

            if (serverSocket != null && !serverSocket.isClosed())  {
                serverSocket.close();
            }

            throw new MyOwnException(e);
        }
    } 
    else {
        if (serverSocket != null && !serverSocket.isClosed())  {
            serverSocket.close();
        }
    }
}

public void stopThread() throws MyOwnException {
    setThreadRunning(false);
}

MainActivity.onPause():

@Override
public void onPause()  {
    super.onPause();

    try {
        udpListener.stopThread();
    } 
    catch (MyOwnException e) {
        //TODO AlertDialog
    }
}

else 分支的 serverSocket.close() 将被执行并导致 LogCat 条目,但是为什么?

更新
也许 Dalvik 认为这是错误,因为 close() 在当前阻塞的每个线程上抛出 SocketExceptions “接收”,但我不确定这个异常是否与这个问题有关。

http://docs .oracle.com/javase/1.4.2/docs/api/java/net/DatagramSocket.html#close()

My MainActivity starts another thread called UdpListener. In onResume, the thread will be started (this works), in onPause it should stop, but socket.close() causes an error message in LogCat "the system call was canceled". The UdpListener itself works fine in JUnit tests so this must be some kind of Android problem.

These are the corresponding methods of my thread:

private void setThreadRunning(boolean running) throws MyOwnException {
    this.running = running;

    if (running) {
        try {
            serverSocket = new DatagramSocket(9800);
        } 
        catch (SocketException e) {
            exceptionThrown = true;
            this.running = false;
            Log.e(TAG, e.getMessage());

            if (serverSocket != null && !serverSocket.isClosed())  {
                serverSocket.close();
            }

            throw new MyOwnException(e);
        }
    } 
    else {
        if (serverSocket != null && !serverSocket.isClosed())  {
            serverSocket.close();
        }
    }
}

public void stopThread() throws MyOwnException {
    setThreadRunning(false);
}

MainActivity.onPause():

@Override
public void onPause()  {
    super.onPause();

    try {
        udpListener.stopThread();
    } 
    catch (MyOwnException e) {
        //TODO AlertDialog
    }
}

serverSocket.close() of else branch will be executed and causes the LogCat entry, but why?

UPDATE
Maybe Dalvik recognize this as error because close() throws SocketExceptions on every thread currently block in "receive", but I'm not sure if this Exception is related to this problem.

http://docs.oracle.com/javase/1.4.2/docs/api/java/net/DatagramSocket.html#close()

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

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

发布评论

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

评论(1

趁年轻赶紧闹 2025-01-04 16:27:58

当我用 SocketException try..catch 包围 run 方法中的 receive() 调用时,错误不再发生。我猜我的异常处理有一些副作用导致了这个错误。

When I surround the receive() call in run method with a SocketException try..catch, the error doesn't occure anymore. I guess that my exception handling had some side effect to cause this error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文