ServerSocket setOption(SocketOption, Object) in Java

setOption(SocketOption, Object): This method is available in the java.net.ServerSocket class of Java.

Syntax:

ServerSocket java.net.ServerSocket.setOption(SocketOption<Object> name, Object value) throws IOException

This method takes two arguments. This method sets the value of a socket option.

Type Parameters: <T> The type of the socket option value.

Parameters: Two parameters are required for this method.

name: The socket option.

value: The value of the socket option. A value of null may be valid for some options.

Returns: this ServerSocket.

Throws:

1. UnsupportedOperationException - if the server socket does not support the option.

2. IllegalArgumentException - if the value is not valid for the option.

3. IOException - if an I/O error occurs, or if the socket is closed.

4. NullPointerException - if the name is null.

5. SecurityException - if a security manager is set and if the socket option requires security permission and if the caller does not have the required permission. StandardSocketOptions does not require any security permission.

Approach 1: UnsupportedOperationException 

Java

package com.ServerSocket;

import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.SocketOption;

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

        int port = 8080, backlog = 5;
        InetAddress bindAddr = InetAddress.getLocalHost();
        ServerSocket serverSocket = new ServerSocket(port, backlog, bindAddr);

        SocketOption<Object> option = new SocketOption<Object>() {

            @Override
            public Class<Object> type() {
                return null;
            }

            @Override
            public String name() {
                return "name";
            }
        };
        serverSocket.setOption(option, null);
        System.out.println("Successfully set option");

        serverSocket.close();
    }
}

Output:

Exception in thread "main" java.lang.UnsupportedOperationException: 'com.ServerSocket.ServerSocketsetOption$1@50cbc42f' not supported at java.base/sun.nio.ch.NioSocketImpl.setOption(NioSocketImpl.java:956) at java.base/java.net.ServerSocket.setOption(ServerSocket.java:1098) at com.ServerSocket.ServerSocketsetOption.main(ServerSocketsetOption.java:27)


Approach 2: NullPointerException

Java

package com.ServerSocket;

import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;

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

        int port = 8080, backlog = 5;
        InetAddress bindAddr = InetAddress.getLocalHost();
        ServerSocket serverSocket = new ServerSocket(port, backlog, bindAddr);

        serverSocket.setOption(null, null);
        System.out.println("Successfully set option");

        serverSocket.close();
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException at java.base/java.util.Objects.requireNonNull(Objects.java:208) at java.base/java.net.ServerSocket.setOption(ServerSocket.java:1095) at com.ServerSocket.ServerSocketsetOption.main(ServerSocketsetOption.java:14)


Some other methods of ServerSocket class

ServerSocket()This method creates an unbound server socket.

ServerSocket(int)This method creates a server socket, bound to the specified port. A port number of 0 means that the port number is automatically allocated, typically from an ephemeral port range. This port number can then be retrieved by calling getLocalPort.

ServerSocket(int, int)This method creates a server socket and binds it to the specified local port number, with the specified backlog. A port number of 0 means that the port number is automatically allocated, typically from an ephemeral port range. This port number can then be retrieved by calling getLocalPort.

ServerSocket(int, int, InetAddress)This method creates a server with the specified port, listens to the backlog, and local IP address to bind to. The bindAddr argument can be used on a multi-homed host for ServerSocket that will only accept connect requests to one of its addresses.

accept()This method listens for a connection to be made to this socket and accepts it. The method blocks until a connection is made.

bind(SocketAddress)This method binds the ServerSocket to a specific address(IP address and port number).

bind(SocketAddress, int)This method binds the ServerSocket to a specific address.

close()This method closes this socket. Any thread currently blocked in accept() will throw a SocketException.

getChannel()This method returns the unique java.nio.channels.ServerSocketChannel object associated with this socket, if any.

getInetAddress()This method returns the local address of this server socket.

getLocalPort()This method returns the port number on which this socket is listening.

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.

getReceiveBufferSize()This method gets the value of the SO_RCVBUF option for this ServerSocket, which is the proposed buffer size that will be used for Sockets accepted from this ServerSocket.

getReuseAddress()This method tests if SO_REUSEADDR is enabled.

getSoTimeout()This method retrieves the setting for SO_TIMEOUT.0 returns implying that the option is disabled (i.e., timeout of infinity). 

isBound()This method returns the binding state of the ServerSocket.

isClosed()This method returns the closed state of the ServerSocket.

setOption(SocketOption, Object)This method sets the value of a socket option.

setPerformancePreferences(int, int, int)This method sets performance preferences for this ServerSocket.

setReceiveBufferSize(int)This method sets a default proposed value for the SO_RCVBUF option for sockets accepted from this ServerSocket.

setReuseAddress(boolean)This method enables/disables the SO_REUSEADDRsocket option. When a TCP connection is closed the connection may remain in a timeout state for a period of time after the connection is closed.

setSocketFactory(SocketImplFactory)This method sets the server socket implementation factory for the application.

setSoTimeout(int)This method enables/disables SO_TIMEOUT with the specified timeout, in milliseconds.

supportedOptions()This method returns a set of the socket options supported by this server socket. This method will continue to return the set of options even after the socket has been closed.

toString()This method returns the implementation address and implementation port of this socket as a String.

No comments:

Post a Comment