简体   繁体   English

Eclipse和多播数据包的奇怪/无法解释的行为

[英]Strange/unexplainable behaviour of Eclipse and multicast packets

I have a very similar behaviour as described here : 我所描述的非常相似的行为在这里

  • running on a Mac Book Pro, Snow Leopard 在Mac Book Pro雪豹上运行
  • using Multicast Sockets to send and receive packets on localhost 使用多播套接字在本地主机上发送和接收数据包

I'm using Eclipse and observed the following behaviour when the client / server are started from within the workspace: 我正在使用Eclipse并从工作空间中启动客户端/服务器时观察到以下行为:

  • if the wireless interface (airport) is up and running, the client does not receive any packets 如果无线接口(机场)启动并运行,客户端没有收到任何数据包
  • if the interface is turned off, everything works as expected 如果接口关闭,则一切正常

But what I don't understand is: 但是我不明白的是:

  • if I create a JAR and run the code in any console -> all good! 如果我创建一个JAR并在任何控制台中运行代码->一切都很好! Just Eclipse seems not to like airport ;-) 只是Eclipse似乎不喜欢机场;-)
  • depending on what wireless network I'm connected to, the above behaviour might change, ie it also works if airport is enabled (for example @ Uni) 根据我连接的无线网络的不同,上述行为可能会发生变化,即,如果启用了机场,它也会起作用(例如,@ Uni)

Does anyone have an idea 'bout this? 有人有想法吗?

Cheers 干杯

Below the straightforward code for server / client: 在服务器/客户端的简单代码下面:

@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();
    }

}

Server: 服务器:

    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();
    }
}

From Class MulticastSocket : 类MulticastSocket

void  joinGroup(SocketAddress mcastaddr, NetworkInterface netIf)
          Joins the specified multicast group at the specified interface.

Try using a specific interface so your joinGroup doesn't fall into the default - which may vary according on available, open ones or due to Eclipse settings. 尝试使用特定的接口,以便您的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) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM