Java Eclipse和多播数据包的奇怪/无法解释的行为
我有一个非常类似的行为,如所述:Java Eclipse和多播数据包的奇怪/无法解释的行为,java,eclipse,sockets,multicast,Java,Eclipse,Sockets,Multicast,我有一个非常类似的行为,如所述: 在Mac Book Pro上运行,雪豹 使用多播套接字在本地主机上发送和接收数据包 我正在使用Eclipse,并在从工作区内启动客户机/服务器时观察到以下行为: 如果无线接口(机场)已启动并正在运行,则客户端不会收到任何数据包 如果关闭了接口,则一切正常 但我不明白的是: 如果我创建一个JAR并在任何控制台中运行代码->一切正常!只是Eclipse似乎不喜欢机场;-) 根据我所连接的无线网络,上述行为可能会发生变化,即,如果机场启用(例如@Uni),它
- 在Mac Book Pro上运行,雪豹
- 使用多播套接字在本地主机上发送和接收数据包
- 如果无线接口(机场)已启动并正在运行,则客户端不会收到任何数据包
- 如果关闭了接口,则一切正常
- 如果我创建一个JAR并在任何控制台中运行代码->一切正常!只是Eclipse似乎不喜欢机场;-)李>
- 根据我所连接的无线网络,上述行为可能会发生变化,即,如果机场启用(例如@Uni),它也会工作
@Override
public void run() {
String multicastAddress = "224.0.0.2";
int multicastPort = 8000;
MulticastSocket socket = null;
try {
try {
InetAddress multicastGoup = InetAddress.getByName(multicastAddress);
socket = new MulticastSocket(multicastPort);
socket.joinGroup(multicastGoup);
} catch (IOException e) {
e.printStackTrace();
return;
}
byte[] buffer = new byte[1024];
while (true) {
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
System.out.println("BEFORE RECEIVE: listening on " + multicastAddress + ":" + multicastPort);
socket.receive(packet);
System.out.println("PACKET RECEIVED");
System.err.println("Client received: " + new String(packet.getData()));
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
socket.close();
}
}
服务器:
public void run() {
MulticastSocket socket = null;
try {
String multicastAddress = "224.0.0.2";
int multicastPort = 8000;
InetAddress multicastGoup = InetAddress.getByName(multicastAddress );
socket = new MulticastSocket(multicastPort);
socket.joinGroup(multicastGoup);
byte[] data = new String("Teststring").getBytes();
while (true) {
socket.send(new DatagramPacket(data, data.length, multicastGoup, multicastPort));
System.out.println("SERVER: Datagram sent");
Thread.sleep(1000);
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
socket.close();
}
}
发件人:
尝试使用一个特定的接口,这样您的joinGroup就不会落入默认值——这可能会根据可用的、打开的接口或Eclipse设置而有所不同
joinGroup
public void joinGroup(SocketAddress mcastaddr,
NetworkInterface netIf)
throws IOException
Joins the specified multicast group at the specified interface.
If there is a security manager, this method first calls its
checkMulticast method with the mcastaddr argument as its argument.
Parameters:
mcastaddr - is the multicast address to join
netIf - specifies the local interface to receive
multicast datagram packets,
-- here is the catch
or null to defer to the interface set by
setInterface(InetAddress) or
setNetworkInterface(NetworkInterface)
发件人:
尝试使用一个特定的接口,这样您的joinGroup就不会落入默认值——这可能会根据可用的、打开的接口或Eclipse设置而有所不同
joinGroup
public void joinGroup(SocketAddress mcastaddr,
NetworkInterface netIf)
throws IOException
Joins the specified multicast group at the specified interface.
If there is a security manager, this method first calls its
checkMulticast method with the mcastaddr argument as its argument.
Parameters:
mcastaddr - is the multicast address to join
netIf - specifies the local interface to receive
multicast datagram packets,
-- here is the catch
or null to defer to the interface set by
setInterface(InetAddress) or
setNetworkInterface(NetworkInterface)
我认为这个问题与
joinGroup()
方法选择的默认接口有关,而这个(默认值)根据Eclipse设置和可用接口的不同而有所不同。在调用joinGroup()
时,是否可以选择要使用的接口,并查看问题是否仍然存在?我认为此问题与joinGroup()方法选择的默认接口有关,并且此接口(默认值)随Eclipse设置和可用接口的不同而不同。调用joinGroup()
时,是否可以选择要使用的接口,并查看问题是否仍然存在?