SocketOptions interface in java

java.net.SocketOptions

The interface of methods to get/set socket options. This interface is implemented by: SocketImpl and DatagramSocketImpl.Subclasses of these should override the methods of this interface in order to support their own options.

The methods and constants that specify this interface's options are for implementation only. If you're not subclassing SocketImpl or DatagramSocketImpl, you won't use these directly. There are type-safe methods to get/set each of these options in Socket, ServerSocket, DatagramSocket, and MulticastSocket.

Declaration

public interface SocketOptions {

    public void setOption(int optID, Object value) throws SocketException;

    public Object getOption(int optID) throws SocketException;

    @Native
    public static final int TCP_NODELAY = 0x0001;

    @Native
    public static final int SO_BINDADDR = 0x000F;

    @Native
    public static final int SO_REUSEADDR = 0x04;

    @Native
    public static final int SO_REUSEPORT = 0x0E;

    @Native
    public static final int SO_BROADCAST = 0x0020;

    @Native
    public static final int IP_MULTICAST_IF = 0x10;

    @Native
    public static final int IP_MULTICAST_IF2 = 0x1f;

    @Native
    public static final int IP_MULTICAST_LOOP = 0x12;

    @Native
    public static final int IP_TOS = 0x3;

    @Native
    public static final int SO_LINGER = 0x0080;

    @Native
    public static final int SO_TIMEOUT = 0x1006;

    @Native
    public static final int SO_SNDBUF = 0x1001;

    @Native
    public static final int SO_RCVBUF = 0x1002;

    @Native
    public static final int SO_KEEPALIVE = 0x0008;

    @Native
    public static final int SO_OOBINLINE = 0x1003;
}


1. SocketOptions.IP_MULTICAST_IF

int java.net.SocketOptions.IP_MULTICAST_IF : 16 [0x10]

Set which outgoing interface on which to send multicast packets. Useful on hosts with multiple network interfaces, where applications want to use other than the system default. Takes/returns an InetAddress.

Valid for Multicast: DatagramSocketImpl

2. SocketOptions.IP_MULTICAST_IF2

int java.net.SocketOptions.IP_MULTICAST_IF2 : 31 [0x1f]

Same as above. This option is introduced so that the behaviour with IP_MULTICAST_IF will be kept the same as before, while this new option can support setting outgoing interfaces with either IPv4 or IPv6 addresses.

3. SocketOptions.IP_MULTICAST_LOOP

int java.net.SocketOptions.IP_MULTICAST_LOOP : 18 [0x12]

This option enables or disables the local loopback of multicast datagrams. This option is enabled by default for Multicast Sockets.

4. SocketOptions.IP_TOS

int java.net.SocketOptions.IP_TOS : 3 [0x3]

This option sets the type-of-service or traffic class field in the IP header for a TCP or UDP socket.

5. SocketOptions.SO_BINDADDR

int java.net.SocketOptions.SO_BINDADDR : 15 [0xf]

Fetch the local address binding of a socket. The default local address of a socket is INADDR_ANY, meaning any local address on a multi-homed host. A multi-homed host can use this option to accept connections to only one of its addresses (in the case of a server socket or DatagramSocket) or to specify its return address to the peer (for a Socket or DatagramSocket). The parameter of this option is an InetAddress.

This option must be specified in the constructor.

Valid for: SocketImpl, DatagramSocketImpl

6. SocketOptions.SO_BROADCAST

int java.net.SocketOptions.SO_BROADCAST : 32 [0x20]

Sets SO_BROADCAST for a socket. This option enables and disables the ability of the process to send broadcast messages. It is supported for only Datagram sockets and only on networks that support the concept of a broadcast message (e.g. Ethernet, token ring, etc.), and it is set by default for DatagramSockets.

7. SocketOptions.SO_KEEPALIVE

int java.net.SocketOptions.SO_KEEPALIVE : 8 [0x8]

When the keep-alive option is set for a TCP socket and no data has been exchanged across the socket in either direction for 2 hours TCP automatically sends a keep-alive probe to the peer. This probe is a TCP segment to which the peer must respond.

Valid only for TCP socket: SocketImpl

8. SocketOptions.SO_LINGER

int java.net.SocketOptions.SO_LINGER : 128 [0x80]

Specify a linger-on-close timeout. This option disables/enables immediate return from a close() of a TCP Socket. Enabling this option with a non-zero Integer timeout means that a close() will block pending the transmission and acknowledgement of all data written to the peer, at which point the socket is closed gracefully. Upon reaching the linger timeout, the socket is closed forcefully, with a TCP RST. Enabling the option with a timeout of zero does a forceful close immediately. If the specified timeout value exceeds 65,535 it will be reduced to 65,535.

Valid only for TCP: SocketImpl

9. SocketOptions.SO_OOBINLINE

int java.net.SocketOptions.SO_OOBINLINE : 4099 [0x1003]

When the OOBINLINE option is set, any TCP urgent data received on the socket will be received through the socket input stream. When the option is disabled (which is the default) urgent data is silently discarded.

10. SocketOptions.SO_RCVBUF

int java.net.SocketOptions.SO_RCVBUF : 4098 [0x1002]

Set a hint of the size of the underlying buffers used by the platform for incoming network I/O. When used in a set, this is a suggestion to the kernel from the application about the size of buffers to use for the data to be received over the socket. When used in get, this must return the size of the buffer actually used by the platform when receiving data on this socket.

Valid for all sockets: SocketImpl, DatagramSocketImpl

11. SocketOptions.SO_REUSEADDR

int java.net.SocketOptions.SO_REUSEADDR : 4 [0x4]

Sets SO_REUSEADDR for a socket. This is used only for MulticastSockets in Java, and it is set by default for MulticastSockets.

Valid for: DatagramSocketImpl

12. SocketOptions.SO_REUSEPORT

int java.net.SocketOptions.SO_REUSEPORT : 14 [0xe]

Sets SO_REUSEPORT for a socket. This option enables and disables the ability to have multiple sockets listen to the same address and port.

Valid for: SocketImpl, DatagramSocketImpl

13. SocketOptions.SO_SNDBUF

int java.net.SocketOptions.SO_SNDBUF : 4097 [0x1001]

Set a hint of the size of the underlying buffers used by the platform for outgoing network I/O. When used in a set, this is a suggestion to the kernel from the application about the size of buffers to use for the data to be sent over the socket. When used in get, this must return the size of the buffer actually used by the platform when sending out data on this socket.

Valid for all sockets: SocketImpl, DatagramSocketImpl

14. SocketOptions.SO_TIMEOUT

int java.net.SocketOptions.SO_TIMEOUT : 4102 [0x1006]

Set a timeout on blocking Socket operations: ServerSocket.accept(); SocketInputStream.read(); DatagramSocket.receive();

The option must be set prior to entering a blocking operation to take effect. If the timeout expires and the operation would continue to block,  java.io.InterruptedIOException is raised. The Socket is not closed in this case.

Valid for all sockets: SocketImpl, DatagramSocketImpl

15. SocketOptions.TCP_NODELAY

int java.net.SocketOptions.TCP_NODELAY : 1 [0x1]

Disable Nagle's algorithm for this connection. Written data to the network is not buffered pending acknowledgement of previously written data.

Valid for TCP only: SocketImpl.

Approach

Java

package com.SocketOptions;

import java.net.SocketOptions;

public class SocketOptionsConstants {
    public static void main(String[] args) {

        System.out.println(SocketOptions.IP_MULTICAST_IF);
        System.out.println(SocketOptions.IP_MULTICAST_IF2);
        System.out.println(SocketOptions.IP_MULTICAST_LOOP);
        System.out.println(SocketOptions.IP_TOS);
        System.out.println(SocketOptions.SO_BINDADDR);
        System.out.println(SocketOptions.SO_BROADCAST);
        System.out.println(SocketOptions.SO_KEEPALIVE);
        System.out.println(SocketOptions.SO_LINGER);
        System.out.println(SocketOptions.SO_OOBINLINE);
        System.out.println(SocketOptions.SO_RCVBUF);
        System.out.println(SocketOptions.SO_REUSEADDR);

        System.out.println(SocketOptions.SO_REUSEPORT);
        System.out.println(SocketOptions.SO_SNDBUF);
        System.out.println(SocketOptions.SO_TIMEOUT);
        System.out.println(SocketOptions.TCP_NODELAY);

    }
}

Output:

16 31 18 3 15 32 8 128 4099 4098 4 14 4097 4102 1


No comments:

Post a Comment