Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Network discovery in Java using multicasting

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 aware of.

I am planning to send a multicast message from client through the network for that specific port to discover the server. However, I'm not too sure how I can find out which IP in my network received my message.

Do I need to create a socket on the client and listen to incoming packets once I send my multicast message in case server replies back?

Thanks in advance.

like image 329
mohi666 Avatar asked Jul 15 '10 18:07

mohi666


People also ask

What is multicasting in Java?

Java Language Networking Multicasting Multicasting is a type of Datagram Socket. Unlike regular Datagrams, Multicasting doesn't handle each client individually instead it sends it out to one IP Address and all subscribed clients will get the message.

What is a multicast socket explain with example?

The multicast datagram socket class is useful for sending and receiving IP multicast packets. A MulticastSocket is a (UDP) DatagramSocket, with additional capabilities for joining "groups" of other multicast hosts on the internet. A multicast group is specified by a class D IP address and by a standard UDP port number.

What is broadcasting in Java?

Broadcasting is a one-to-all type of communication, i.e. the intention is to send the datagram to all the nodes in the network. Unlike in the case of point-to-point communication, we don't have to know the target host's IP Address. Instead, a broadcast address is used.


1 Answers

(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.

like image 52
irreputable Avatar answered Sep 22 '22 05:09

irreputable