getOption(SocketOption): This method is available in the java.net.DatagramSocket class of Java.
Syntax:
<?> ? java.net.DatagramSocket.getOption(SocketOption<?> name) throws IOException
This method takes one argument. This method returns the value of a socket option.
Type Parameters: <T> The type of the socket option value.
Parameters: One parameter is required for this method.
name: The socket option.
Returns: The value of the socket option.
Throws:
1. UnsupportedOperationException - if the datagram socket does not support the option.
2. IOException - if an I/O error occurs, or if the socket is closed.
3. NullPointerException - if name is null.
4. SecurityException - if a security manager is set and if the socket option requires a security permission and if the caller does not have the required permission. StandardSocketOptions do not require any security permission.
Approach 1: UnsupportedOperationException
Java
package com.DatagramSocket;import java.io.IOException;import java.net.DatagramSocket;import java.net.InetAddress;import java.net.SocketOption;public class DatagramSocketgetOption {public static void main(String[] args) throws IOException {int port = 100;InetAddress laddr = InetAddress.getLocalHost();DatagramSocket datagramSocket =new DatagramSocket(port, laddr);SocketOption<?> name = new SocketOption<>() {@Overridepublic String name() {return "name";}@Overridepublic Class<Object> type() {return null;}};datagramSocket.getOption(name);System.out.println("Successfully get option");}}
Output:
Exception in thread "main" java.lang.UnsupportedOperationException: 'com.DatagramSocket.DatagramSocketgetOption$1@721e0f4f' not supported at java.base/sun.nio.ch.DatagramChannelImpl.getOption(DatagramChannelImpl.java:408) at java.base/sun.nio.ch.DatagramSocketAdaptor.getOption(DatagramSocketAdaptor.java:424) at java.base/java.net.DatagramSocket.getOption(DatagramSocket.java:1027) at com.DatagramSocket.DatagramSocketgetOption.main(DatagramSocketgetOption.java:29)
Approach 2: NullPointerException
Java
package com.DatagramSocket;import java.io.IOException;import java.net.DatagramSocket;import java.net.InetAddress;import java.net.SocketOption;public class DatagramSocketgetOption {public static void main(String[] args) throws IOException {int port = 100;InetAddress laddr = InetAddress.getLocalHost();DatagramSocket datagramSocket =new DatagramSocket(port, laddr);SocketOption<?> name = null;datagramSocket.getOption(name);System.out.println("Successfully get option");}}
Output:
Exception in thread "main" java.lang.NullPointerException at java.base/java.util.Objects.requireNonNull(Objects.java:208) at java.base/sun.nio.ch.DatagramChannelImpl.getOption(DatagramChannelImpl.java:406) at java.base/sun.nio.ch.DatagramSocketAdaptor.getOption(DatagramSocketAdaptor.java:424) at java.base/java.net.DatagramSocket.getOption(DatagramSocket.java:1027) at com.DatagramSocket.DatagramSocketgetOption.main(DatagramSocketgetOption.java:16)
Some other methods of DatagramSocket class
DatagramSocket(): This method constructs a datagram socket and binds it to any available port on the local host machine.
DatagramSocket(int): This method constructs a datagram socket and binds it to the specified port on the local host machine.
DatagramSocket(SocketAddress): This method creates a datagram socket, bound to the specified local socket address.
DatagramSocket(int, InetAddress): This method creates a datagram socket, bound to the specified local address.
bind(SocketAddress): This method binds this DatagramSocket to a specific address and port.
connect(SocketAddress): This method connects this socket to a remote socket address (IP address + port number).
connect(InetAddress, int): This method connects the socket to a remote address for this socket. When a socket is connected to a remote address, packets may only be sent to or received from that address.
disconnect(): This method disconnects the socket.
getBroadcast(): This method tests if SO_BROADCAST is enabled.
getChannel(): This method returns the unique java.nio.channels.DatagramChannel object associated with this datagram socket, if any.
getInetAddress(): This method returns the address to which this socket is connected. Returns null if the socket is not connected.
getLocalAddress(): This method gets the local address to which the socket is bound.
getLocalPort(): This method returns the port number on the local host to which this socket is bound.
getLocalSocketAddress(): This method returns the address of the endpoint this socket is bound to.
getOption(SocketOption): This method returns the value of a socket option.
getPort(): This method returns the port number to which this socket is connected.
getReceiveBufferSize(): This method gets the value of the SO_RCVBUF option for this DatagramSocket, which is the buffer size used by the platform for input on this DatagramSocket.
getRemoteSocketAddress(): This method returns the address of the endpoint this socket is connected to, or null if it is unconnected.
getReuseAddress(): This method tests if SO_REUSEADDR is enabled.
getSendBufferSize(): This method gets the value of the SO_SNDBUF option for this DatagramSocket, which is the buffer size used by the platform for output on this DatagramSocket.
getSoTimeout(): This method retrieves the setting for SO_TIMEOUT. 0 returns implies that the option is disabled (i.e., timeout of infinity).
getTrafficClass(): This method gets the traffic class or type-of-service in the IP datagram header for packets sent from this DatagramSocket.
isBound(): This method returns the binding state of the socket.
isClosed(): This method returns whether the socket is closed or not.
isConnected(): This method returns the connection state of the socket.
receive(DatagramPacket): This method receives a datagram packet from this socket.
send(DatagramPacket): This method sends a datagram packet from this socket.
setBroadcast(boolean): This method enables/disables SO_BROADCAST.
DatagramSocket.setDatagramSocketImplFactory(DatagramSocketImplFactory): This method sets the datagram socket implementation factory for the application.
setOption(SocketOption, String): This method sets the value of a socket option.
setReceiveBufferSize(int): This method sets the SO_RCVBUF option to the specified value for this DatagramSocket.
setReuseAddress(boolean): This method enables/disables the SO_REUSEADDR socket option.
setSendBufferSize(int): This method sets the SO_SNDBUF option to the specified value for this DatagramSocket.
setSoTimeout(int): This method enables/disable SO_TIMEOUT with the specified timeout, in milliseconds.
setTrafficClass(int): This method sets the traffic class or type-of-service octet in the IP datagram header for datagrams sent from this DatagramSocket.
supportedOptions(): This method returns a set of the socket options supported by this socket. This method will continue to return the set of options even after the socket has been closed.
No comments:
Post a Comment