gpt4 book ai didi

java - Android SocketException 'Socket is closed' 即使在使用 socket.isClosed() 后也已关闭

转载 作者:行者123 更新时间:2023-11-30 02:49:29 25 4
gpt4 key购买 nike

我有一个应用程序,我在其中使用套接字来监听来自服务器的消息,它有两个 Activity ,每个 Activity 都有自己的方法来处理消息。

当我从第一个开始第二个时,我关闭了该 Activity 的套接字监听器,并在第二个 Activity 的 onCreate 方法中启动了一个新的监听器。但是,当我切换 Activity 时,我收到一个 java.net.SocketException: Socket is closed 错误。

public synchronized void run(){
//Check if the thread has been shut down
while(!this.stopped){
socket = null;
try{
//Socket
socket = new DatagramSocket(port);
//Packet
byte[] data = new byte [1024];
DatagramPacket packet = new DatagramPacket(data, data.length);
if(!socket.isClosed()){
//Store data from socket into packet
socket.receive(packet);
//Create a string from the data
String received = new String(packet.getData(),packet.getOffset(),packet.getLength());

//Log the string TODO remove this
Log.i("RECEIVED", received);

//Get a new message object from the handler
Message msg = commandHandler.obtainMessage();
//Store the string in the message
msg.obj = received;
//Send the message to the handler
commandHandler.sendMessage(msg);
}

}catch (IOException e) {
e.printStackTrace();
}finally{
if(socket != null)
socket.close();
}
}
}

/**
* Close the listener
*/
public void shutDown(){
this.stopped = true;
if(socket != null){
socket.close();
}
}

从上面可以看出,我使用 !socket.isClosed() 在接收消息之前检查套接字是否关闭

错误轨迹:

06-27 19:48:12.129: W/System.err(19460): java.net.SocketException: Socket closed
06-27 19:48:12.129: W/System.err(19460): at libcore.io.Posix.recvfromBytes(Native Method)
06-27 19:48:12.129: W/System.err(19460): at libcore.io.Posix.recvfrom(Posix.java:136)
06-27 19:48:12.129: W/System.err(19460): at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:164)
06-27 19:48:12.129: W/System.err(19460): at libcore.io.IoBridge.recvfrom(IoBridge.java:513)
06-27 19:48:12.129: W/System.err(19460): at java.net.PlainDatagramSocketImpl.doRecv(PlainDatagramSocketImpl.java:161)
06-27 19:48:12.129: W/System.err(19460): at java.net.PlainDatagramSocketImpl.receive(PlainDatagramSocketImpl.java:169)
06-27 19:48:12.129: W/System.err(19460): at java.net.DatagramSocket.receive(DatagramSocket.java:253)
06-27 19:48:12.129: W/System.err(19460): at com.android.homeservice.server.TabletListener.run(TabletListener.java:54)

更新

事实证明,我在第二个 Activity 中调用了线程的 start() 方法两次,一次在 onCreate 中,另一次在 onStart 这是以前版本的代码遗留下来的。无论如何,感谢您的所有回答,如果我浪费了您的时间,我深表歉意

最佳答案

我建议重新架构。让套接字处理全部在线程上,只告诉线程什么时候应该退出。当线程退出时,关闭套接字。

public synchronized void run(){
//Check if the thread has been shut down
Socket socket = new DatagramSocket(port);
while(!this.stopped){
socket = null;
try{
//Socket
//Packet
byte[] data = new byte [1024];
//Store data from socket into packet
socket.receive(packet);
//Create a string from the data
String received = new String(packet.getData(),packet.getOffset(),packet.getLength());

//Log the string TODO remove this
Log.i("RECEIVED", received);

//Get a new message object from the handler
Message msg = commandHandler.obtainMessage();
//Store the string in the message
msg.obj = received;
//Send the message to the handler
commandHandler.sendMessage(msg);
}

}catch (IOException e) {
e.printStackTrace();
}finally{
if(socket != null)
socket.close();
}
}
socket.close()
}

/**
* Close the listener
*/
public void shutDown(){
this.stopped = true;

}

关于java - Android SocketException 'Socket is closed' 即使在使用 socket.isClosed() 后也已关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24459181/

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