Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java多播套接字接收器在两台机器上不工作_Java_Windows_Sockets_Multicast_Multicastsocket - Fatal编程技术网

Java多播套接字接收器在两台机器上不工作

Java多播套接字接收器在两台机器上不工作,java,windows,sockets,multicast,multicastsocket,Java,Windows,Sockets,Multicast,Multicastsocket,我正在尝试在internet上实现多播通信。这是我的密码 先发送后接收5次代码: public static void main(String args[]) throws IOException { MulticastSocket socket = new MulticastSocket(4446); InetAddress group = InetAddress.getByName("228.5.6.7"); socket.join

我正在尝试在internet上实现多播通信。这是我的密码

先发送后接收5次代码:

public static void main(String args[]) throws IOException
    {
        MulticastSocket socket = new MulticastSocket(4446);
        InetAddress group = InetAddress.getByName("228.5.6.7");
        socket.joinGroup(group);
        socket.setNetworkInterface(NetworkInterface.getByInetAddress(group));
        DatagramPacket packet;
        System.out.println("Trasmitter Started!!");
        for (int i = 0; i < 5; i++)
        {
            String buf = "Hi receiver";
            packet = new DatagramPacket(buf.getBytes(), buf.getBytes().length,group,4446);
            socket.send(packet);
            System.out.println("Packet Sent!");
            byte[] buff = new byte[256];
            packet = new DatagramPacket(buff,buff.length);
            socket.receive(packet);
            String received = new String(packet.getData());
            System.out.println("Quote of the Moment: " + received);
        }
        socket.leaveGroup(group);
        socket.close();
    }
publicstaticvoidmain(字符串args[])引发IOException
{
MulticastSocket=新的MulticastSocket(4446);
InetAddress组=InetAddress.getByName(“228.5.6.7”);
插座组(组);
setNetworkInterface(NetworkInterface.getByInetAddress(组));
数据包;
System.out.println(“Trasmitter启动!!”;
对于(int i=0;i<5;i++)
{
字符串buf=“Hi receiver”;
packet=newdatagrampacket(buf.getBytes(),buf.getBytes().length,group,4446);
socket.send(包);
System.out.println(“数据包已发送!”);
字节[]buff=新字节[256];
数据包=新数据包(buff,buff.length);
套接字接收(数据包);
接收到的字符串=新字符串(packet.getData());
System.out.println(“当前报价:+已收到”);
}
插座组(对照组);
socket.close();
}
仅接收:

public static void main(String args[]) throws IOException
    {
        System.setProperty("java.net.preferIPv4Stack", "true");
        MulticastSocket socket = new MulticastSocket(4446);
        InetAddress group = InetAddress.getByName("228.5.6.7");
        //socket.setNetworkInterface(NetworkInterface.getByInetAddress(group));
        socket.setInterface(group);
        socket.joinGroup(group);

        DatagramPacket packet;
        System.out.println("Receiver Started!!");
        for (int i = 0; i < 5; i++)
        {
            byte[] buff = new byte[256];
            packet = new DatagramPacket(buff,buff.length);
            socket.receive(packet);
            String received = new String(packet.getData());
            System.out.println("Received Message: " + received);
        }
        socket.leaveGroup(group);
        socket.close();
    }
publicstaticvoidmain(字符串args[])引发IOException
{
setProperty(“java.net.preferIPv4Stack”,“true”);
MulticastSocket=新的MulticastSocket(4446);
InetAddress组=InetAddress.getByName(“228.5.6.7”);
//setNetworkInterface(NetworkInterface.getByInetAddress(组));
socket.setInterface(组);
插座组(组);
数据包;
System.out.println(“接收器已启动!!”;
对于(int i=0;i<5;i++)
{
字节[]buff=新字节[256];
数据包=新数据包(buff,buff.length);
套接字接收(数据包);
接收到的字符串=新字符串(packet.getData());
System.out.println(“接收到的消息:“+接收到的”);
}
插座组(对照组);
socket.close();
}
代码在一台机器上工作,但当我尝试在另一台机器上运行接收器时,它没有收到任何东西。我不会走错方向吧?我搜索了一些解决方案,他们说要将networkInterface添加到套接字,但这也不起作用。
操作系统:Windows 8.1
我也在使用代理(如果这可能是问题的话)


摆脱这条线。您正在将接口设置为组地址,这没有意义。我很惊讶它没有抛出异常

即使没有那条线,它也不起作用。。。我搜索了一会儿这个问题,人们说要在套接字中添加networkInterface。。。但它仍然不工作,所以代理是问题所在,或者说是它的另一个症状。如果这两台计算机不在同一子网中,则取决于路由器转发多播。这两台计算机在同一子网中。那我该怎么办?是否有一些工作或其他我缺少的东西。
socket.setNetworkInterface(...);