MulticastSocket(int): This method is available in the java.net.MulticastSocket class of Java.
Syntax:
java.net.MulticastSocket.MulticastSocket(int port) throws IOException
This method takes one argument. This method constructs a multicast socket and binds it to the specified port on the local host machine. The socket will be bound to the wildcard address.
Parameters: One parameter is required for this method.
port: port to use.
Throws:
1. IOException - if an I/O exception occurs while creating the Multicast.
2. SocketSecurityException - if a security manager exists and its checkListen method doesn't allow the operation.
3. IllegalArgumentException - if the port is out of range.
Approach 1: When no exception
Java
package com.MulticastSocket;import java.io.IOException;import java.net.MulticastSocket;public class MulticastSocket2 {public static void main(String[] args) throws IOException {int port = 8080;MulticastSocket multicastSocket = new MulticastSocket(port);System.out.println(multicastSocket);}}
Output:
java.net.MulticastSocket@63d4e2ba
Approach 2: IllegalArgumentException
Java
package com.MulticastSocket;import java.io.IOException;import java.net.MulticastSocket;public class MulticastSocket2 {public static void main(String[] args) throws IOException {int port = 98080;MulticastSocket multicastSocket = new MulticastSocket(port);System.out.println(multicastSocket);}}
Output:
Exception in thread "main" java.lang.IllegalArgumentException: port out of range:98080 at java.base/java.net.InetSocketAddress.checkPort(InetSocketAddress.java:153) at java.base/java.net.InetSocketAddress.<init>(InetSocketAddress.java:198) at java.base/java.net.InetSocketAddress.<init>(InetSocketAddress.java:176) at java.base/java.net.MulticastSocket.<init>(MulticastSocket.java:202) at com.MulticastSocket.MulticastSocket2.main(MulticastSocket2.java:10)
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