Java DatagramSocket将数据包发送到255.255.255.255失败

时间:2011-07-05 07:42:18

标签: java udp broadcast datagram

我正在使用java编写网络程序,我想将一些数据包发送到255.255.255.255,但它失败了,即使我将它们发送到192.168.1.255,根据ifconfig命令的输出,它是广播地址。但是当我把它们发送给我的队友的IP时,它运行正常。

这是我程序的代码:

public class StackOverFlow {
    public static void main(String[] args) {
        Network net= new Network();

        Scanner input= new Scanner(System.in);
        while(input.hasNext())
          net.sendMessage(input.nextLine());
    }
}

我已经使用DatagarmSocket和DatagramPacket来实现这一点,这是我对网络的实现:

class Network {
DatagramSocket socket;

public Network() {
    try {
        socket = new DatagramSocket(8027);
        socket.setBroadcast(true);
        socket.connect(InetAddress.getByName("255.255.255.255"), 8027);
    } catch (Exception e) {
        System.err.println("Connection failed. " + e.getMessage());
    }
    listen();
}

public void listen() {
    new Thread() {
        public void run() {
            while (true) {
                try {
                    byte[] buf = new byte[1000];
                    DatagramPacket packet = new DatagramPacket(buf,
                            buf.length);
                    socket.receive(packet);
                    String message = new String(buf);
                    System.out.println("Recieved: " + message);
                    if (message.equals("end"))
                        return;
                } catch (Exception e) {
                    System.err.println(e.getMessage());
                }
            }
        }
    }.start();
}

public void sendMessage(String message){
    byte[] buf= message.getBytes();

    DatagramPacket packet= new DatagramPacket(buf, buf.length);
    try{
        socket.send(packet);
    }catch(Exception e){
        System.err.println("Sending failed. " + e.getMessage());
    }
}

没有例外被抛出。
我在一个临时网络中 我正在使用MAC OS X 10.6,而我的伙伴正在使用kubuntu 11.04。 这是ifconfig输出:

lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
inet6 ::1 prefixlen 128 
inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1 
inet 127.0.0.1 netmask 0xff000000 
gif0: flags=8010<POINTOPOINT,MULTICAST> mtu 1280
stf0: flags=0<> mtu 1280

en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
inet6 fe80::21f:f3ff:fed5:4779%en0 prefixlen 64 scopeid 0x4 
inet 192.168.1.1 netmask 0xffffff00 broadcast 192.168.1.255
ether 00:1f:f3:d5:47:79 
media: autoselect (100baseTX <full-duplex>) status: active
supported media: autoselect 10baseT/UTP <half-duplex> 10baseT/UTP <full-duplex> 10baseT/UTP     <full-duplex,hw-loopback> 10baseT/UTP <full-duplex,flow-control> 100baseTX <half-    duplex> 100baseTX <full-duplex> 100baseTX <full-duplex,hw-loopback> 100baseTX <full-duplex,flow-control> 1000baseT <full-duplex> 1000baseT <full-duplex,hw-loopback> 1000baseT <full-duplex,flow-control> none

en1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
inet6 fe80::21d:4fff:feff:2b4d%en1 prefixlen 64 scopeid 0x5 
inet 213.233.170.97 netmask 0xfffffc00 broadcast 213.233.171.255
ether 00:1d:4f:ff:2b:4d 
media: autoselect status: active
supported media: autoselect

fw0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 2030
lladdr 00:21:e9:ff:fe:bc:79:b2 
media: autoselect <full-duplex> status: inactive
supported media: autoselect <full-duplex>

en2: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
ether 00:1f:f3:b6:2c:be 
media: autoselect status: inactive
supported media: none autoselect 10baseT/UTP <half-duplex>

vmnet1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
inet 192.168.149.1 netmask 0xffffff00 broadcast 192.168.149.255
ether 00:50:56:c0:00:01 

vmnet8: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
inet 192.168.73.1 netmask 0xffffff00 broadcast 192.168.73.255
ether 00:50:56:c0:00:08 

en0是我用来连接我的伴侣的设备。

请简单,我是新手:)

提前致谢。

4 个答案:

答案 0 :(得分:10)

使用广播时,您需要启用它

socket.setBroadcast(true);

另一件事是,如果两台计算机位于两个不同的网络中,则必须确保路由器配置正确。通常默认情况下不会路由广播。此外,如果您的路由器具有无线接口和有线接口,则如果未启用广播,则这些广播可能无效(默认情况下可能存在在这两个接口之间转发广播的硬件)。

答案 1 :(得分:5)

不是将DatagramSocket连接到广播地址,而是构造DatagramPacket来定位它,即

DatagramPacket dp = new DatagramPacket(byteArray, byteArray.length, InetAddress.getByName("255.255.255.255"), yourPortNumber);

和魔术一样,你发了一个广播。然后在另一边抓住它,只需要听那个端口:

DatagramSocket dsock = new DatagramSocket(samePortUsedAbove);
DatagramPacket dp = new DatagramPacket(byteArray, byteArray.length);
dsock.receive(dp);

答案 2 :(得分:3)

192.168.1.255 
  • 请检查网络中的子网掩码。 您的发送机器和接收机器可能不属于同一网络。
  • 请检查网络中是否存在接收设备。
  • 如果您的机器之间有路由器,我认为不会传输该消息。

答案 3 :(得分:1)

如果我没记错,you cannot receive from broadcast-adresses,但只发给他们!因此,在接收方,您必须专门听取“您的”IP。