Class MuiticastSocket in Java

MuiticastSocket in Java

The MulticastSocket class implements packet-oriented, connectionless, multicast data communication. In Internet parlance, this is the User Datagram Protocol (UDP) with additional functionality for joining and leaving groups of other multicast hosts on the Internet. A multicast group is specified by a Class D address.

This subclass of DatagramSocket is used to send and receive multicast UDP packets. It extends DatagramSocket by adding joinGroup() and leaveGroup() methods to join and leave multicast groups. The IP address specified to these methods should be a valid multicast address in the range of 224. 0. 0. 1 to 239. 255. 255. 255. We do not have to join a group to send a packet to a multicast address, but we must join the group to receive packets sent to that address.

Ads code goes here

MulticastSocket defines a variant send() method that allows us to specify a time to – live (TTL) value for the packet we send. This value specifies the number of network ” hops ” the packet may travel before it expires. we can also set a default TTL for all packets sent through a MulticastSocket with setTTLO.

MulticastSocket inherits most of its functionality from DatagramSocket; it adds the ability to join and leave multicast groups. When a MulticastSocket joins a group, it receives all of the packets destined for the group. Any DatagramSocket or MulticastSocket can send packets to a multicast group.

Untrusted applets are not allowed to use multicast sockets.

READ  Literals in Java Simplified

The class structure of MuiticastSocket is given as:

public class java.net.MulticastSocket extends java.net.DatagramSocket{
// Public Constructors  
public MulticastSocket() throws IOException;
public MulticastSocket(int port) throws IOException;
// Public Instance Methods 
public InetAddress getInterface() throws SocketException;
public byte getTTL() throws IOException;
public void joinGroup(InetAddress mcastaddr) throws IOException;
public void leaveGroup(InetAddress mcastaddr) throws IOException;
public synchronized void send(DatagramPacket p, byte ttl) throws IOException; 
public void setInterface (InetAddress inetAddr) throws SocketException;
public void setTTL(byte ttl) throws IOException;
}

The details of the class structure are given as follows:

public MulticastSocket();

public MulticastSocket() constructor creates a MulticastSocket that is bound to any available port on the local host machine.

public MulticastSocket(int port);

public MulticastSocket(int port) method constructor creates a MulticastSocket that is bound to the given port on the local host machine.

Parameter
port – A port number.

public InetAddress getInterface();

public InetAddress getInterface() method returns the IP address that this MulticastSocket uses to send out packets to multicast destinations.

This method returns the address of the network interface used for outgoing multicast packets.

public byte getTTL();

public byte getTTL() method returns the TTL value for this socket. This value is the number of hops an outgoing packet can traverse before it is discarded.

This method returns the time-to-live (TTL) value for this socket.

public void joinGroup(InetAddress mcastaddr);

public void joinGroup(InetAddress mcastaddr) method is used to join a multicast group. An exception is thrown if the given address is not a multicast address. While the socket is part of a group, it receives all the packets that are sent to the group.

Parameter
mcastaddr – The IP address of the group to join.

public void leaveGroup(InetAddress mcastaddr)

public void leaveGroup(InetAddress mcastaddr) method is used to leave a multicast group. An exception is thrown if the given address is not a multicast address.

READ  How To Format Output in Java?

Parameter
mcastaddr – The IP address of the group to leave.

public synchronized void send(DatagramPacket p, byte ttl);

public synchronized void send(DatagramPacket p, byte ttl) method sends a packet from this socket using the given TTL value. The packet data, packet length, destination address, and destination port number are specified by the given DatagramPacket.

Generally, it is easier to use setTTL() to set the TTL value for the socket, then use send(DatagramPacket) to send data. This method is provided for special cases.

Parameter
p – The DatagramPacket to be sent.
ttl – The time-to-live (TTL) value for this packet.

public void setInterface(InetAddress inf);

public void setInterface(InetAddress inf) method is used to set the address that is used for outgoing multicast packets.

Parameter
inf – The new address of the network interface for multicast packets.

public void setTTL(byte ttl);

public void setTTL(byte ttl) method is used to set the TTL value of the socket. The TTL value is the number of hops an outgoing packet can traverse before it is discarded.

Parameter
ttl – The new time-to-live (TTL) value for this socket.

Apart from these, the MuiticastSocket class also has inherited methods from class- Object. They are as follows:

  • clone()
  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • toString()
  • wait(long)

The MuiticastSocket class also has inherited methods from DatagramSocket. They are as follows:

  • getLocalPort()
  • send(DatagramPacket)
  • close()
  • getLocalAddress()
  • getSoTimeout()
  • receive(DatagramPacket)
  • setSoTimeout(int)

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *