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

时间:2011-03-23 05:42:37

标签: java eclipse sockets multicast

我的行为与here所描述的非常相似:

  • 在Mac Book Pro,Snow Leopard上运行
  • 使用多播套接字在localhost上发送和接收数据包

我正在使用Eclipse,并在工作区内启动客户端/服务器时发现以下行为:

  • 如果无线接口(机场)启动并运行,客户端接收任何数据包
  • 如果界面关闭,一切都按预期工作

但我不明白的是:

  • 如果我创建一个JAR并在任何控制台中运行代码 - >都好! Just 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();
    }
}

1 个答案:

答案 0 :(得分:1)

来自Class MulticastSocket

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

尝试使用特定的界面,以使您的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)