Javatpoint Logo
Javatpoint Logo

java.net.MulticastSocket class in Java

The MulticastSocket class in Java is a subclass of the DatagramSocket class used to send and receive multicast datagrams. Multicast datagrams are a type of datagram that is sent to a group of hosts on a network. The MulticastSocket class provides methods for joining and leaving multicast groups and sending and receiving multicast datagrams.

To create a MulticastSocket object, specify the multicast group address and port number. You can then use the MulticastSocket object to send and receive multicast datagrams.

The MulticastSocket class is a powerful tool for sending and receiving multicast datagrams. It can be used to implement various applications, such as chat, file-sharing, and streaming applications.

Constructors:

public MulticastSocket(): The MulticastSocket class in Java is used for sending and receiving IP multicast datagrams. It provides functionality to join and leave multicast groups and send/receive multicast packets.

Syntax:

public MulticastSocket(int port): The public MulticastSocket(int port) method is a constructor in the Java.net.MulticastSocket class that is used to create a new MulticastSocket object that is bound to the specified port. The constructor does not take any other arguments.

Syntax:

Parameter:

port : The port number to which the MulticastSocket object should be bound.

public MulticastSocket(SocketAddress bindaddr): The MulticastSocket(SocketAddress bindaddr) constructor in the MulticastSocket class is used to create a MulticastSocket object and bind it to the specified SocketAddress.

Syntax:

Parameters:

bindaddr : The socket address to which the MulticastSocket object should be bound.

Methods :

setTTL (deprecated): The setTTL(int ttl) method in the MulticastSocket class (deprecated) was used to set the time-to-live (TTL) value for multicast packets. The TTL value determined how far multicast packets could propagate in the network.

Syntax:

Parameters:

ttl : the time to live

setTimeToLive(): The setTimeToLive(int ttl) method in the MulticastSocket class is used to set the time-to-live (TTL) value for multicast packets. The TTL value determines how far multicast packets can propagate in the network.

The TTL value must be between 0 and 255, inclusive. If the TTL value exceeds this range, an IllegalArgumentException will be thrown. Multicast packets sent with a TTL of 0 are not transmitted on the network but may be delivered locally.

Syntax:

Parameters:

ttl : the time to live

getTTL (deprecated): The getTimeToLive() method returns the default time-to-live for multicast packets sent out on the socket.

Syntax:

Returns: the default time-to-live value

Throws:

IOException: The I/O error occurs while getting the default time-to-live value; the getTTL() method throws an IOException.

getTimeToLive(): Get the default time-to-live for multicast packets sent out on the socket.

Syntax:

Returns: the default time-to-live value

Throws:

IOException: The I/O exception occurs while getting the default time-to-live value.

joinGroup(): The joinGroup() method in the Java.net.MulticastSocket class joins the specified multicast group on the local system. The multicast group is identified by the InetAddress object that is passed to the joinGroup() method.

Syntax:

Parameters:

group: The InetAddress object that identifies the multicast group to join.

ttl: The TTL value for multicast packets sent to the multicast group.

Throws:

IOException: If there is an error joining the multicast group.

SecurityException: If the security manager does not allow the operation.

The joinGroup() method has another overloaded version that allows you to specify the network interface to use when joining the multicast group.

Syntax:

Parameters:

mcastaddr : The SocketAddress object that identifies the multicast group to join.

netIf: The NetworkInterface object specifies the network interface to use when joining the multicast group.

Throws:

IOException: If there is an error joining the multicast group.

SecurityException: If the security manager does not allow the operation.

leaveGroup(): It allows a MulticastSocket to stop receiving multicast packets from the specified multicast group.

Syntax:

Parameters:

mcastaddr : The InetAddress object that identifies the multicast group to leave.

Throws:

IOException: If there is an error leaving the multicast group.

SecurityException: If the security manager does not allow the operation.

The leaveGroup() method has another overloaded version that allows you to specify the network interface on which to leave the multicast group.

Syntax:

Parameters:

mcastaddr : The SocketAddress object that identifies the multicast group to leave.

netIf: The NetworkInterface object that specifies the network interface on which to leave the multicast group.

Throws:

IOException: If there is an error leaving the multicast group.

SecurityException: If the security manager does not allow the operation.

setInterface(): The setInterface() method in Java is used to set the network interface that a MulticastSocket will use to send and receive multicast packets.

Syntax:

Parameters:

inf: The InetAddress object specifies the network interface that should be used.

Throws:

SocketException: If there is an error in the underlying protocol, such as a TCP error.

getInterface(): The getInterface() method in Java is used to get the network interface that a MulticastSocket currently uses to send and receive multicast packets. The method returns an InetAddress object that represents the network interface.

Syntax:

Throws:

SocketException: If there is an error in the underlying protocol, such as a TCP error.

setNetworkInterface(): Specify the network interface for outgoing multicast datagrams sent on this socket.

Syntax:

Parameters:

netIf: The NetworkInterface object specifies the network interface that should be used.

Throws:

SocketException: If there is an error in the underlying protocol, such as a TCP error.

getNetworkInterface(): The getNetworkInterface() method returns the network interface to send packets from this socket.

Syntax:

Throws:

SocketException: If there is an error in the underlying protocol, such as a TCP error.

setLoopbackMode(): The setLoopbackMode() method in Java turns the loopback mode on or off for a MulticastSocket. The loopback mode is enabled by default. When the loopback mode is enabled, the socket receives multicast packets sent by the socket.

It is used to specify whether the multicast packets will be looped back to local socket.

Syntax:

Parameters:

disable: A boolean value specifies whether the loopback mode should be enabled or disabled.

Throws:

SocketException: If there is an error in the underlying protocol, such as a TCP error.

getLoopbackMode(): Get the setting for whether or not the socket will receive its multicast packets.

Syntax:

Throws:

SocketException: If there is an error in the underlying protocol, such as a TCP error.

send() (deprecated): The send() method in Java is used to send a packet to a multicast group. It allows for the specification of a time-to-live (TTL) value for the outgoing packet other than the default value specified using the setTimeToLive() method. The send() method does not alter the default TTL value but only uses the specified value for the current packet.

Syntax:

Parameters:

p: The datagram packet to be sent.

ttl: The time-to-live (TTL) value for the packet.

Throws:

IOException: If an error occurs while sending the packet.

Java program to illustrate various MulticastSocket classes.

Filename: CustomMulticastSocketExample

Output:

Network Interface: name:lo (lo)
Received: Hello, multicast!






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA