我正在研究 MulticastSocket。在 AIX 7 上使用 Websphere 8.0.0.5 进行测试。问题是从发件人那里收到错误消息。测试场景为:1.运行sendMulticast(新线程) 2.运行receiveMulticast(新线程) 3.运行receiveMulticast(新线程)
第一个接收者线程总是接收到正确的消息。辅助接收线程接收到错误消息。
但有趣的是,我在使用 Java 6 作为控制台应用程序的同一台机器(AIX 7)上进行了尝试,一切正常。
这是代码:
static final int port = 9999;
static final String multicastAddress = "224.2.2.3";
public static void sendMulticast() {
MulticastSocket socket = null;
DatagramPacket outPacket = null;
byte[] outBuf;
long t= System.currentTimeMillis();
//run 60 seconds
long end = t+60000;
try {
socket = new MulticastSocket();
String msg;
while(System.currentTimeMillis() < end) {
msg = "//172.21.131.242:51860/SERVICE_EXECUTION_CACHE|//172.21.131.242:51860/ERROR_CODES_CACHE|//172.21.131.242:51860/ACCOUNTING_CACHE|//172.21.131.242:51860/CONFIGURATION_ITEMS_CACHE|//172.21.131.242:51860/REFERENCE_DATA";
outBuf = msg.getBytes();
InetAddress address = InetAddress.getByName(multicastAddress);
outPacket = new DatagramPacket(outBuf, outBuf.length, address, port);
socket.setTimeToLive(32);
socket.send(outPacket);
System.out.println("Server sends : " + msg);
try {
Thread.sleep(500);
} catch (InterruptedException ie) {
}
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
socket.close();
}
public static void receiveMulticast() {
MulticastSocket socket = null;
DatagramPacket packet = null;
byte[] inBuf = new byte[1500];
long t= System.currentTimeMillis();
//run 60 seconds
long end = t+60000;
try {
// Prepare to join multicast group
socket = new MulticastSocket(port);
InetAddress address = InetAddress.getByName(multicastAddress);
socket.joinGroup(address);
while(System.currentTimeMillis() < end) {
packet = new DatagramPacket(inBuf, inBuf.length);
socket.receive(packet);
byte[] payload = packet.getData();
String msg = new String(payload);
System.out.println("From " + packet.getAddress() + " Msg : " + msg);
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
socket.close();
}
这是日志:您可以在 10:39:47:675 和 10:39:48:177 看到丢失的字符串正在发送到接收器。
可能是什么问题呢?
[2/20/13 10:39:45:162 EET] 0000003a SystemOut O Server sends : //172.21.131.242:51860/SERVICE_EXECUTION_CACHE|//172.21.131.242:51860/ERROR_CODES_CACHE|//172.21.131.242:51860/ACCOUNTING_CACHE|//172.21.131.242:51860/CONFIGURATION_ITEMS_CACHE|//172.21.131.242:51860/REFERENCE_DATA
[2/20/13 10:39:45:163 EET] 0000003b SystemOut O From /172.21.131.242 Msg : //172.21.131.242:51860/SERVICE_EXECUTION_CACHE|//172.21.131.242:51860/ERROR_CODES_CACHE|//172.21.131.242:51860/ACCOUNTING_CACHE|//172.21.131.242:51860/CONFIGURATION_ITEMS_CACHE|//172.21.131.242:51860/REFERENCE_DATA
[2/20/13 10:39:45:665 EET] 0000003b SystemOut O From /172.21.131.242 Msg : //172.21.131.242:51860/SERVICE_EXECUTION_CACHE|//172.21.131.242:51860/ERROR_CODES_CACHE|//172.21.131.242:51860/ACCOUNTING_CACHE|//172.21.131.242:51860/CONFIGURATION_ITEMS_CACHE|//172.21.131.242:51860/REFERENCE_DATA
[2/20/13 10:39:45:665 EET] 0000003a SystemOut O Server sends : //172.21.131.242:51860/SERVICE_EXECUTION_CACHE|//172.21.131.242:51860/ERROR_CODES_CACHE|//172.21.131.242:51860/ACCOUNTING_CACHE|//172.21.131.242:51860/CONFIGURATION_ITEMS_CACHE|//172.21.131.242:51860/REFERENCE_DATA
[2/20/13 10:39:46:167 EET] 0000003a SystemOut O Server sends : //172.21.131.242:51860/SERVICE_EXECUTION_CACHE|//172.21.131.242:51860/ERROR_CODES_CACHE|//172.21.131.242:51860/ACCOUNTING_CACHE|//172.21.131.242:51860/CONFIGURATION_ITEMS_CACHE|//172.21.131.242:51860/REFERENCE_DATA
[2/20/13 10:39:46:168 EET] 0000003b SystemOut O From /172.21.131.242 Msg : //172.21.131.242:51860/SERVICE_EXECUTION_CACHE|//172.21.131.242:51860/ERROR_CODES_CACHE|//172.21.131.242:51860/ACCOUNTING_CACHE|//172.21.131.242:51860/CONFIGURATION_ITEMS_CACHE|//172.21.131.242:51860/REFERENCE_DATA
[2/20/13 10:39:46:672 EET] 0000003a SystemOut O Server sends : //172.21.131.242:51860/SERVICE_EXECUTION_CACHE|//172.21.131.242:51860/ERROR_CODES_CACHE|//172.21.131.242:51860/ACCOUNTING_CACHE|//172.21.131.242:51860/CONFIGURATION_ITEMS_CACHE|//172.21.131.242:51860/REFERENCE_DATA
[2/20/13 10:39:46:673 EET] 0000003b SystemOut O From /172.21.131.242 Msg : //172.21.131.242:51860/SERVICE_EXECUTION_CACHE|//172.21.131.242:51860/ERROR_CODES_CACHE|//172.21.131.242:51860/ACCOUNTING_CACHE|//172.21.131.242:51860/CONFIGURATION_ITEMS_CACHE|//172.21.131.242:51860/REFERENCE_DATA
[2/20/13 10:39:47:173 EET] 0000003a SystemOut O Server sends : //172.21.131.242:51860/SERVICE_EXECUTION_CACHE|//172.21.131.242:51860/ERROR_CODES_CACHE|//172.21.131.242:51860/ACCOUNTING_CACHE|//172.21.131.242:51860/CONFIGURATION_ITEMS_CACHE|//172.21.131.242:51860/REFERENCE_DATA
[2/20/13 10:39:47:174 EET] 0000003b SystemOut O From /172.21.131.242 Msg : //172.21.131.242:51860/SERVICE_EXECUTION_CACHE|//172.21.131.242:51860/ERROR_CODES_CACHE|//172.21.131.242:51860/ACCOUNTING_CACHE|//172.21.131.242:51860/CONFIGURATION_ITEMS_CACHE|//172.21.131.242:51860/REFERENCE_DATA
[2/20/13 10:39:47:674 EET] 0000003a SystemOut O Server sends : //172.21.131.242:51860/SERVICE_EXECUTION_CACHE|//172.21.131.242:51860/ERROR_CODES_CACHE|//172.21.131.242:51860/ACCOUNTING_CACHE|//172.21.131.242:51860/CONFIGURATION_ITEMS_CACHE|//172.21.131.242:51860/REFERENCE_DATA
[2/20/13 10:39:47:675 EET] 0000003b SystemOut O From /46.50.52.50 Msg : CE_EXECUTION_CACHE|//172.21.131.242:51860/ERROR_CODES_CACHE|//172.21.131.242:51860/ACCOUNTING_CACHE|//172.21.131.242:51860/CONFIGURATION_ITEMS_CACHE|//172.21.131.242:51860/REFERENCE_DATA131.242:51860/REFERENCE_DATA
[2/20/13 10:39:47:676 EET] 0000003c SystemOut O From /172.21.131.242 Msg : //172.21.131.242:51860/SERVICE_EXECUTION_CACHE|//172.21.131.242:51860/ERROR_CODES_CACHE|//172.21.131.242:51860/ACCOUNTING_CACHE|//172.21.131.242:51860/CONFIGURATION_ITEMS_CACHE|//172.21.131.242:51860/REFERENCE_DATA
[2/20/13 10:39:48:176 EET] 0000003a SystemOut O Server sends : //172.21.131.242:51860/SERVICE_EXECUTION_CACHE|//172.21.131.242:51860/ERROR_CODES_CACHE|//172.21.131.242:51860/ACCOUNTING_CACHE|//172.21.131.242:51860/CONFIGURATION_ITEMS_CACHE|//172.21.131.242:51860/REFERENCE_DATA
[2/20/13 10:39:48:177 EET] 0000003b SystemOut O From /46.50.52.50 Msg : CE_EXECUTION_CACHE|//172.21.131.242:51860/ERROR_CODES_CACHE|//172.21.131.242:51860/ACCOUNTING_CACHE|//172.21.131.242:51860/CONFIGURATION_ITEMS_CACHE|//172.21.131.242:51860/REFERENCE_DATA131.242:51860/REFERENCE_DATA