gpt4 book ai didi

java - Android多播 - 有时接收阻塞线程

转载 作者:行者123 更新时间:2023-11-29 22:02:53 27 4
gpt4 key购买 nike

我认为 MulticastSocket.receive 方法阻塞了我的线程。它不会再次工作,除非应用程序转到 onResume(),这会重新启动线程。我该如何解决这个问题?

接收者代码

private class ReceiverThread extends Thread
{

private static final String TAG = ComMessageReceiver.TAG + "Thread";

private WifiManager wifiManager;
private MulticastSocket multicastSocket;
private InetSocketAddress groupInetSocketAddress;
private boolean joinedGroup = false;

public ReceiverThread(String group, int port, int timeout) throws IOException {

super();

wifiManager = (WifiManager) App.context.getSystemService(Context.WIFI_SERVICE);
groupInetSocketAddress = new InetSocketAddress(InetAddress.getByName(group), port);
multicastSocket = new MulticastSocket(port);
multicastSocket.setSoTimeout(timeout);
}

public ReceiverThread() throws IOException {

this(Config.multicastAddress, Config.multicastPort, DEFAULT_TIMEOUT);
}

@Override
public void run() {

Log.d(TAG, "started");

while (!this.isInterrupted()) {

if (wifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLED) {

if (!joinedGroup) {

try {
multicastSocket.joinGroup(groupInetSocketAddress,
NetworkInterface.getByInetAddress(getWifiInetAddress()));

wifiManager.createMulticastLock("ComMessageReceiverLock").acquire();

joinedGroup = true;

} catch (IOException ex) {

Log.e(TAG, "Failed to join Multicast group: " + ex.getMessage());
}
}

try {

byte[] buffer = new byte[256];

DatagramPacket packet = new DatagramPacket(buffer, buffer.length);

multicastSocket.receive(packet);

Log.d(TAG, "Message received: " + message.message);

} catch (SocketTimeoutException e) {
} catch (IOException ex) {

multicastSocket = null;

Log.e(TAG, ex.getMessage());
}
} else
joinedGroup = false;
}
}

}

多播代码

private class ComServerThread extends Thread {

private static final String LOG = "ComServerThread";
private final static long INTERVAL = 1000;
private boolean alert = true;

private DatagramSocket socket;

public ComServerThread() {

try {

socket = new DatagramSocket(5500);
} catch (SocketException ex) {

Log.e(LOG, "Error opening socket: " + ex.getMessage());
}
}

@Override
public void run() {

while (!this.isInterrupted()) {

try {

try {

sleep(INTERVAL);
} catch (InterruptedException ex) {

Log.e(LOG, "Interrupted the thread sleep! not cool: "
+ ex.getMessage());
}

byte[] buffer;

String msg = "Test"

buffer = msg.getBytes();

InetAddress group = InetAddress.getByName("230.0.0.1");

DatagramPacket packet = new DatagramPacket(buffer,
buffer.length, group, 5500);
socket.send(packet);

Log.d(LOG, "Packet sent.");

} catch (UnknownHostException ex) {

Log.e(LOG, "Oops. Invalid host: " + ex.getMessage());
} catch (IOException ex) {

Log.e(LOG,
"Something bad happened while sending the packet. Take a look: "
+ ex.getMessage());
}
}
}
}

最佳答案

你说的对,是一种阻塞方式。

如果您设置读取超时,它会抛出一个您似乎忽略的 SocketTimeoutException,因此您的代码将再次循环并阻塞。

你不需要每次循环都加入多播组:那只是浪费。

关于java - Android多播 - 有时接收阻塞线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11512663/

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