Open In App

Java.net.MulticastSocket class in Java

This class is used for sending and receiving multicast IP packets. It extends DatagramSocket class and provides additional functionality for joining groups. A message sent to the group IP address will be received by all the clients who have joined the group. This must be kept in mind that for sending packets to the group, datagram socket doesn’t have to join the group but for receiving the packets addressed to the group, it must join the group. This class provides various methods for controlling the flow of multicast packets like setting the ttl, network interface to use etc, along with the major functions of joining and leaving a group.
Constructors : 

Syntax :public MulticastSocket() 
Syntax :public MulticastSocket(int port) 
Parameters : 
port : port number to bind this socket to
Syntax :public MulticastSocket(SocketAddress bindaddr) 
Parameters : 
bindaddr : Socket address to bind this socket to

Methods :



Syntax :public void setTTL(byte ttl)
Parameters :
ttl : the time to live
Syntax :public void setTimeToLive(int ttl)
Parameters :
ttl : the time to live
Syntax :public byte getTTL()
Syntax :public void getTimeToLive()
Syntax :public void joinGroup(InetAddress mcastaddr)
               throws IOException
Parameters :
mcastaddr : multicast address to join
Throws :
IOException : if error occurs while joining or the address is
not a multicast address.

         Another overloaded method which also specifies the network interface to use while joining the group.

Syntax :public void joinGroup(SocketAddress mcastaddr,
             NetworkInterface netIf)
               throws IOException
Parameters :
mcastaddr : multicast address to join
netIf : network interface to use while joining
Throws :
IOException : if error occurs while joining or the address is
not a multicast address.
Syntax :public void leaveGroup(InetAddress mcastaddr)
               throws IOException
Parameters :
mcastaddr : multicast address to leave
Throws :
IOException : if error occurs while leaving

         Another overloaded method for leaving the group which also allows for specification of network interface on which to leave the group.



Syntax :public void leaveGroup(SocketAddress mcastaddr,
             NetworkInterface netIf)
               throws IOException
Parameters :
mcastaddr : multicast address to leave
netIf : network interface on which to leave
Throws :
IOException : if error occurs while leaving
Syntax :public void setInterface(InetAddress inf)
                  throws SocketException
Parameters :
inf : the inetaddress
Throws :
SocketException : if error in underlying protocol
Syntax :public InetAddress getInterface()
                         throws SocketException
Throws :
SocketException : if error in underlying protocol
Syntax :public void setNetworkInterface(NetworkInterface netIf)
                         throws SocketException
Parameters :
netIf : network interface to use
Throws :
SocketException : if error in underlying protocol
Syntax :public NetworkInterface getNetworkInterface()
                                     throws SocketException
Throws :
SocketException : if error in underlying protocol
Syntax : public void setLoopbackMode(boolean disable)
                     throws SocketException
Parameters :
disable : true or false
Syntax : public vboolean getLoopbackMode()
                     throws SocketException
Syntax :public void send(DatagramPacket p,
                   byte ttl)
          throws IOException
Parameters :
p : datagram packet to send
ttl : the time to live
Throws :
IOException : If error occurs while setting the ttl

Java Implementation : 




//Java program to illustrate various
//MulticastSocket class methods
import java.io.IOException;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.NetworkInterface;
import java.util.Enumeration;
 
public class multisock
{
    public static void main(String[] args) throws IOException
    {
         
        MulticastSocket ms = new MulticastSocket();
        InetAddress ip = InetAddress.getByName("224.168.1.124");
 
        // setTTL() method
        // this method is deprecated and instead
        // use setTimeToLive() method
        // Un-commenting below line would throw
        // a warning as the method is deprecated
        ms.setTTL((byte) 25);
 
        // setTimeToLive() method, will override
        // setting by setTTL() method
        ms.setTimeToLive(20);
 
        // getTTL() method
        // deprecated, so use getTimeToLive() method instead
        System.out.println("TTL : " + ms.getTTL());
 
        // getTimeToLive() method
        System.out.println("Time to Live : " +
                             ms.getTimeToLive());
 
        NetworkInterface nif = NetworkInterface.getByIndex(1);
        Enumeration<InetAddress> enu = nif.getInetAddresses();
        InetAddress intadd = enu.nextElement();
 
        // setInterface() method
        ms.setInterface(intadd);
 
        // getInterface() method
        System.out.println("Interface : " + ms.getInterface());
 
        // setNetworkInterface() method
        ms.setNetworkInterface(nif);
 
        // getNetworkInterface() method
        System.out.println("Network Interface : " +
                           ms.getNetworkInterface());
 
        // setLoopbackMode() method
        ms.setLoopbackMode(true);
 
        // getLoopbackMode() method
        System.out.println("Loopback mode : " +
                            ms.getLoopbackMode());
    }
 
}

Output : 

TTL : 20
Time to Live : 20
Interface : /127.0.0.1
Network Interface : name:lo (Software Loopback Interface 1)
Loopback mode : true

For a detailed implementation of a multicast socket application with methods to joinGroup() and send() packets, please refer to A group chat application in java
References : 
Official Java Documentation 


Article Tags :