MulticastSocket setTimeToLive(int) in Java

setTimeToLive(int): This method is available in the java.net.MulticastSocket class of Java.

Syntax:

void java.net.MulticastSocket.setTimeToLive(int ttl) throws IOException

This method takes one argument. This method sets the default time-to-live for multicast packets sent out to this MulticastSocket in order to control the scope of the multicast.

The TTL must be in the range 0 <= ttl <=255 or an IllegalArgumentException will be thrown. Multicast packets sent with a TTL of 0 are not transmitted on the network but may be delivered locally.

Parameters: One parameter is required for this method.

ttl: the time-to-live.

Throws:

1. IOException - if an I/O exception occurs while setting the default time-to-live value.

Approach

Java

package com.MulticastSocket;

import java.io.IOException;
import java.net.MulticastSocket;

public class MulticastSocketsetTimeToLive {
    public static void main(String[] args) throws IOException {

        MulticastSocket multicastSocket = new MulticastSocket();

        int ttl = 100;

        multicastSocket.setTimeToLive(ttl);
        System.out.println("Successfully setTimeToLive");
    }
}

Output:

Successfully setTimeToLive


Some other methods of MulticastSocket class

MulticastSocket()This method constructs a multicast socket and binds it to any available port on the local host machine. 

MulticastSocket(int)This method constructs a multicast socket and binds it to the specified port on the local host machine.

MulticastSocket(SocketAddress)This method creates a multicast socket, bound to the specified local socket address.

getNetworkInterface()This method gets the multicast network interface set.

getTimeToLive()This method gets the default time-to-live for multicast packets sent out on the socket.

joinGroup(SocketAddress, NetworkInterface)This method joins the specified multicast group at the specified interface.

leaveGroup(SocketAddress, NetworkInterface)This method leaves a multicast group on a specified local interface.

setNetworkInterface(NetworkInterface)This method specifies the network interface for outgoing multicast datagrams sent on this socket.

setTimeToLive(int)This method sets the default time-to-live for multicast packets sent out to this MulticastSocket in order to control the scope of the multicast.

No comments:

Post a Comment