我尝试使用端口 5500 将多播主机设置为 230.0.0.1。然后,在另一边,我说在端口 5500 加入组 230.0.0.1。它加入并接收数据包几秒钟。然后它突然停止了。如果我使用 255.255.255.255 它会正常接收数据包。为什么会这样?多播发送方的代码如下:
private class StatusBroadcasterThread extends Thread
{
private static final boolean DEBUG = App.DEBUG;
private static final String TAG = "StatusBroadcasterThread";
private DatagramSocket broadcastSocket;
public StatusBroadcasterThread(int port) throws SocketException {
broadcastSocket = new DatagramSocket(port);
this.start();
}
@Override
public void run() {
while (!this.isInterrupted()) {
try {
byte[] buffer = status.toString().getBytes();
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, InetAddress.getByName(App.Config.multicastAddress),
App.Config.multicastPort);
broadcastSocket.send(packet);
if (DEBUG)
Log.d(TAG, "Sent: " + new String(packet.getData()));
} catch (IOException e) {
Log.e(TAG, "Error: " + e.getMessage());
}
try {
sleep(App.Config.broadcastInterval);
} catch (InterruptedException ex) {
}
}
}
}
接收线程:
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.createWifiLock(WifiManager.WIFI_MODE_FULL, TAG);
wifiManager.createMulticastLock(TAG).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);
Message message = new Message(packet);
Log.d(TAG, "message from " + message.getIp() + " " + message.getMsg());
for (MessageListener listener : listenerList)
listener.onMessageReceived(message);
} catch (SocketTimeoutException ex) {
Log.e(TAG, "Timed out: " + ex.getMessage());
} catch (IOException ex) {
Log.e(TAG, ex.getMessage());
}
} else
joinedGroup = false;
}
}
InetAddress getWifiInetAddress() throws UnknownHostException {
ByteBuffer wifiRawAddress = ByteBuffer.allocate(4);
wifiRawAddress.order(ByteOrder.LITTLE_ENDIAN).putInt(wifiManager.getConnectionInfo().getIpAddress());
return InetAddress.getByAddress(wifiRawAddress.array());
}
}