Network discovery in Java using multicasting

前端 未结 4 746
挽巷
挽巷 2020-12-08 11:52

I\'m trying to make a client/server Java App. Both client and server will be running on the same wi-fi network. Server will be running on a specific port that client is awar

4条回答
  •  囚心锁ツ
    2020-12-08 12:00

    (1)server listens on a pre-arranged port

    DatagramSocket s = new DatagramSocket(8888);
    s.receive  //(1)
    s.send     //(2)
    

    (3)client sends a message to the port, on the broadcast IP, 255.255.255.255

    DatagramSocket c = new DatagramSocket();
    c.send(255.255.255.255:8888,msg)     //(3)
    c.receive  //(4)
    

    the client binds to a port too. we didn't specify it, so it's random chosen for us.

    (3) will broadcast the message to all local machines, server at (1) receives message, with the client IP:port.

    (2) server sends response message to client IP:port

    (4) client gets the reponse message from server.

提交回复
热议问题