简体   繁体   English

Java多播套接字在Windows上什么都没有收到

[英]Java Multicast Socket doesn't receive anything on Windows

I need to send and receive in multicast. 我需要以多播发送和接收。

This is my Sender: 这是我的发件人:

public static void main(String[] args) {

    MulticastSocket socket = null;
    try {
        socket = new MulticastSocket(3575);
        int n = 1;
        while (n <= 100) {
            byte[] buf = new byte[256];
            // non aspetta la richiesta
            String dString = new Date().toString();
            buf = dString.getBytes();

            // invia il messaggio in broadcast
            InetAddress group = InetAddress.getByName("230.0.0.1");
            DatagramPacket packet = new DatagramPacket(buf, buf.length, group, 3575);
            socket.send(packet);
            System.out.println ("Broadcasting: "+dString);
            Thread.sleep(1000);
            n++;
        }
        socket.close();
    }catch(Exception e) { e.printStackTrace(); socket.close();}

}//main

This is my Receiver: 这是我的接收者:

public static void main(String[] args) throws IOException {

    MulticastSocket socket = new MulticastSocket(3575);
    InetAddress group = InetAddress.getByName("230.0.0.1");
    socket.joinGroup(group);
    DatagramPacket packet;
    for (int i = 0; i < 100; i++) {
        byte[] buf = new byte[256];
        packet = new DatagramPacket(buf, buf.length);
        socket.receive(packet);
        String received = new String(packet.getData()).trim();
        System.out.println("Time: " + received);
    }
    socket.leaveGroup(group);
    socket.close();
}//main

When I run them, the Receiver does not receive anything. 当我运行它们时,接收器什么也没收到。 I tried on two different PC ( both with Windows) with AntiVirus and firewall disabled. 我尝试在两台不同的PC(都装有Windows)上禁用了AntiVirus和防火墙。 I also tried with different LAN: my router, my phone hotspot. 我还尝试了不同的LAN:路由器,电话热点。 It does not work neither on local machine. 在本地计算机上也不起作用。

How can I solve the problem? 我该如何解决这个问题? Thanks 谢谢

I compiled and ran your code on my laptop, with the sender and receiver on the same machine. 我在便携式计算机上编译并运行了您的代码,发送方和接收方在同一台计算机上。 It works. 有用。 (Fedora 26 Linux, Java 1.8.0_171) (Fedora 26 Linux,Java 1.8.0_171)

It seems that the problem is something to do with your networking, not the application code. 看来问题出在您的网络上,而不是应用程序代码上。 So, since this not a programming problem, I think you would be better off asking this Question on the ServerFault site ... where they specialize in networking, etcetera. 因此,由于这不是编程问题,所以我认为您最好在ServerFault网站上询问此问题,他们专门研究网络等。

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

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