- how to identify other computers in the network (with same application running)
- how to get the IP address of those computers
- how to tell other computers in the network, "I am here".
It is simple. We do it this way; (i) Each computer (or mobile) shouts that it is in the network (ii) Others listen to this. We make use of multicasting for this purpose. Multicasing is same as sending UDP datagrams. But still there are some minor differences. In java, we use MulticastSocket instead DatagramSocket. MulticastSocket has additional facilities to join or leave multicast groups. MulticastSocket class helps for sending and receiving IP
multicast packets. In multicasting, we send DatagramPackets to one of the multicast addresses. The multicast addresses are class D IP addresses ranging from 224.0.0.0 (exclusive) to 239.255.255.255 (inclusive). A multicast group is determined by a multicast ip address and a port number. Any computer can send packets to a multicast group. But to receive packets in a multicast group, you should join that group. Really, we can't call this a broadcast. Broadcast is the term used to describe communication where packet(s) are sent from one or more devices to all other devices in the network. Multicast means sending packets from one or more devices to a set of other devices determined by a logical group (not all others).
For example, consider a group 239.0.0.73:19360. Suppose, we have chat servers and chat clients. Chat clients search for chat servers to join a chat. So the chat servers should send UDP packets (Datagram Packets) to 239.0.0.73:19360, no matter on which port the server has opened the multicast socket. But, for the receivers (clients in this example), should open multicast sockets with port number 19360 and join multicast group 239.0.0.73 to receive datagrams sent to multicast group 239.0.0.73:19360. In the following java code, we will see how to do this.
Sender
try{ MulticastSocket mcs=new MulticastSocket(); //creating multicast socket //port number is assigned by the OS mcs.setTimeToLive(5); String decl="!!Chatting%%"+System.getProperty("user.name")+"^^";//broadcast message DatagramPacket pkt=new DatagramPacket(decl.getBytes(),decl.length(),InetAddress.getByName("239.0.0.73"),19360);//multicast packet Socket s=null; //TCP socket for chatting ServerSocket ss=new ServerSocket(9999); //TCP Serversocket to accept connection request from client ss.setSoTimeout(500); boolean connected=false; do{ try{ mcs.send(pkt); System.out.println("sending: "+decl); s=ss.accept(); connected=true; System.out.println("connected to"+s); }catch(SocketTimeoutException eee){} }while(connected==false); }catch(Exception ex){ex.printStackTrace();}
Program explanation
line 1: The sender creates a MulticastSocket at random port. The port number is assigned by OS. Port number doesn't matter since sender only needs to send packet to the multicast group, and don't want to receive from.
line 3: Sets time to live (TTL) for the multicast packets.It is the maximum number routers the packets can go through. This value is decremented at every router on the route to its destination. If the TTL field reaches zero before the datagram arrives at its destination, the packet will be dropped. If there are 10 routers between the sender and the receiver, the TTL value should be at least 10 for the packet to reach its destination.
line 4: The content of packet. Here we use !!Chatting string determine whether it is our chatting application sending this packet. We also sends the username of the sender.
line 5: Creating the datagram packet with the message. destination ip and port are set to our multicast group's ip and port.
line 7,8: We use the socket and ServerSocket for TCP chat communication later. This depends on your goal.
line 14: Sending the multicast datagram packet.
Receiver
The following is the java code to receive multicast messages (datagram packets).
try{ MulticastSocket mcs=new MulticastSocket(19360); mcs.joinGroup(InetAddress.getByName("239.0.0.73")); byte[] bu=new byte[100]; DatagramPacket dp=new DatagramPacket(bu,100); mcs.receive(dp); InetAddress ip=dp.getAddress(); String sss=new String(dp.getData()); System.out.println("Server found:"+sss.substring(sss.indexOf("%%")+2,sss.indexOf("^^"))); s=new Socket(ip,9999); }catch(Exception ex){ex.printStackTrace();}
Program explanation
line 2: The client creates a MulticastSocket at port number 19360. It is the port number of our multicast group.line 3: Joined the group using the ip 239.0.0.73.
line 6: Receiving packet.
line 7: Retrieving IP address of sender (server).
line 8: Retrieving message from datagram packet.
line 9: Extracting server users name from message
line 10: Establishing TCP connection with server using the retrieved IP