Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
161 views
in Technique[技术] by (71.8m points)

java - Sending Unicast packets to a Multicast port

I have a Multicast socket open and is receiving Multicast message. From this thread, it seems that the same multicast socket should also be able to receive unicast messages. However, I'm not able to get anything.

Edit: the port number seems the be problem. Port 3702 is used by ws-discovery for unicasting which is related to what I'm trying to do. I'm tracking down a problem where the client's probe to the service is not caught by the service's multicast socket. I'm running this on windows.

My multicast server:

class Server extends Thread {
MulticastSocket multicastSocket;
final Logger LOG;
final int PORT = 3702;
final String MULTICAST_ADDR = "239.255.255.250";
InetAddress multicastGroup;

public Server() {
    LOG = Logger.getLogger("Server");
    try {
        multicastGroup = InetAddress.getByName(MULTICAST_ADDR);
        multicastSocket = new MulticastSocket(PORT);
        multicastSocket.setInterface(InetAddress.getLocalHost());
        multicastSocket.joinGroup(multicastGroup);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

@Override
public void run() {
    while (!Global.exit) {
        byte[] buf = new byte[1000];
        DatagramPacket recv = new DatagramPacket(buf, buf.length);
        try {
            multicastSocket.receive(recv);
            String msg = new String(recv.getData(), StandardCharsets.UTF_8);
            LOG.log(Level.INFO, "got: " + msg);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

And the client code:

public void directMsgTest(){
    try {
        DatagramSocket datagramSocket = new DatagramSocket( 8080,InetAddress.getLocalHost());
        String msg = "direct msg";
        byte[] buf = msg.getBytes();
        DatagramPacket packet = new DatagramPacket(buf, buf.length, InetAddress.getLocalHost(), DST_PORT);
        datagramSocket.send(packet);
        datagramSocket.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
question from:https://stackoverflow.com/questions/65650109/sending-unicast-packets-to-a-multicast-port

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It seems that this is Window's fault. It uses WS discovery in some of its services, thus using port 3702 and eating unicast packets send to port 3702 instead of giving it to my server.

I tried running this on Linux and it was fine.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...