gpt4 book ai didi

android - 无法在 Android 中关闭服务器套接字的 DataInputStream

转载 作者:行者123 更新时间:2023-11-29 01:32:13 25 4
gpt4 key购买 nike

在销毁我的应用程序时,我想关闭与服务器的套接字连接并关闭 DataInputStream。所以我在我的 MainActivity 中放置了以下代码。

public void onDestroy()
{
Log.d("TAG", "DESTROYING");
Read_table.flag=false;
try
{
Read_table.soc.close();
Read_table.input.close();

} catch (IOException e)
{ Log.d("TAG", "Exception");

e.printStackTrace();
}
super.onDestroy();
}

ReadTable 是一个服务,它有一个从服务器读取数据的线程。当我从托盘中删除此应用程序时,主 Activity 执行的 onDestroy() 方法。

但在服务器端,我仍然能够写入 DataInputStream,并且没有 IOException,因此我可以将其从服务器内存中删除。

最佳答案

But on the Server side I am still able to write to the DataInputStream and there is no IOException

对于一些写入,是的,但是当您应该关闭套接字时,您最终会得到 IOException: connection reset by peer。这主要是因为发送方的套接字发送缓冲区。

如果服务器从套接字读取,它就会到达流的结尾。

关于android - 无法在 Android 中关闭服务器套接字的 DataInputStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30552398/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com