Top Description Fields Methods
java.net

public Interface SocketOptions

Known Direct Implementers
java.net.DatagramSocketImpl, java.net.SocketImpl
Imports
java.lang.annotation.Native

Interface of methods to get/set socket options. This interface is implemented by SocketImpl and DatagramSocketImpl. Subclasses of these two classes should override the getOption(int) and setOption(int, Object) methods of this interface in order to support their own options.

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

Author
David Brown
Since
1.1

Field Summary

Modifier and TypeField and Description
public static final int
IP_MULTICAST_IF

See StandardSocketOptions#IP_MULTICAST_IF for description of this socket option.

public static final int
IP_MULTICAST_IF2

This option is used to both set and fetch the outgoing interface on which the multicast packets are sent.

public static final int
IP_MULTICAST_LOOP

See StandardSocketOptions#IP_MULTICAST_LOOP for description of this socket option.

public static final int
IP_TOS

See StandardSocketOptions#IP_TOS for description of this socket option.

public static final int
SO_BINDADDR

Fetch the local address binding of a socket.

public static final int
SO_BROADCAST

See StandardSocketOptions#SO_BROADCAST for description of this socket option.

public static final int
SO_KEEPALIVE

See StandardSocketOptions#SO_KEEPALIVE for description of this socket option.

public static final int
SO_LINGER

See StandardSocketOptions#SO_LINGER for description of this socket option.

public static final int
SO_OOBINLINE

When this option is set, any TCP urgent data received on the socket will be received through the socket input stream.

public static final int
SO_RCVBUF

See StandardSocketOptions#SO_RCVBUF for description of this socket option.

public static final int
SO_REUSEADDR

See StandardSocketOptions#SO_REUSEADDR for description of this socket option.

public static final int
SO_REUSEPORT

See StandardSocketOptions#SO_REUSEPORT for description of this socket option.

public static final int
SO_SNDBUF

See StandardSocketOptions#SO_SNDBUF for description of this socket option.

public static final int
SO_TIMEOUT

This option is used to both set and fetch a timeout value on blocking Socket operations:

public static final int
TCP_NODELAY

See StandardSocketOptions#TCP_NODELAY for description of this socket option.

Method Summary

Modifier and TypeMethod and Description
public Object

Returns:

the value of the option
getOption
(int
an int identifying the option to fetch
optID
)

Fetch the value of an option.

public void
setOption(int
identifies the option
optID
,
Object
the parameter of the socket option
value
)

Enable/disable the option specified by optID.

Field Detail

IP_MULTICAST_IFback to summary
public static final int IP_MULTICAST_IF

See StandardSocketOptions#IP_MULTICAST_IF for description of this socket option.

Annotations
@Native
See Also
MulticastSocket#setInterface(InetAddress), MulticastSocket#getInterface()
IP_MULTICAST_IF2back to summary
public static final int IP_MULTICAST_IF2

This option is used to both set and fetch the outgoing interface on which the multicast packets are sent. Useful on hosts with multiple network interfaces, where applications want to use other than the system default. This option supports setting outgoing interfaces with either IPv4 and IPv6 addresses.

Annotations
@Native
Since
1.4
See Also
MulticastSocket#setNetworkInterface(NetworkInterface), MulticastSocket#getNetworkInterface()
IP_MULTICAST_LOOPback to summary
public static final int IP_MULTICAST_LOOP

See StandardSocketOptions#IP_MULTICAST_LOOP for description of this socket option.

Annotations
@Native
Since
1.4
IP_TOSback to summary
public static final int IP_TOS

See StandardSocketOptions#IP_TOS for description of this socket option.

Annotations
@Native
Since
1.4
SO_BINDADDRback to summary
public static final int SO_BINDADDR

Fetch the local address binding of a socket. This option cannot be set and can only be fetched. 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 ServerSocket or DatagramSocket), or to specify its return address to the peer (for a Socket or DatagramSocket). The type of this option's value is an InetAddress.

Annotations
@Native
See Also
Socket#getLocalAddress, DatagramSocket#getLocalAddress
SO_BROADCASTback to summary
public static final int SO_BROADCAST

See StandardSocketOptions#SO_BROADCAST for description of this socket option.

Annotations
@Native
Since
1.4
SO_KEEPALIVEback to summary
public static final int SO_KEEPALIVE

See StandardSocketOptions#SO_KEEPALIVE for description of this socket option.

Annotations
@Native
See Also
Socket#setKeepAlive, Socket#getKeepAlive
SO_LINGERback to summary
public static final int SO_LINGER

See StandardSocketOptions#SO_LINGER for description of this socket option.

Set the value to Boolean.FALSE or an integer less than 0 with setOption(int, Object) to disable this option. An integer greater than or equal to 0 will enable the option and will represent the linger interval.

If this option is enabled then getOption(int) will return an integer value representing the linger interval, else the return value will be Boolean.FALSE.

Annotations
@Native
See Also
Socket#setSoLinger, Socket#getSoLinger
SO_OOBINLINEback to summary
public static final int SO_OOBINLINE

When this 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.

Annotations
@Native
See Also
Socket#setOOBInline, Socket#getOOBInline
SO_RCVBUFback to summary
public static final int SO_RCVBUF

See StandardSocketOptions#SO_RCVBUF for description of this socket option.

Annotations
@Native
See Also
Socket#setReceiveBufferSize, Socket#getReceiveBufferSize, DatagramSocket#setReceiveBufferSize, DatagramSocket#getReceiveBufferSize
SO_REUSEADDRback to summary
public static final int SO_REUSEADDR

See StandardSocketOptions#SO_REUSEADDR for description of this socket option.

Annotations
@Native
SO_REUSEPORTback to summary
public static final int SO_REUSEPORT

See StandardSocketOptions#SO_REUSEPORT for description of this socket option.

Annotations
@Native
Since
9
SO_SNDBUFback to summary
public static final int SO_SNDBUF

See StandardSocketOptions#SO_SNDBUF for description of this socket option.

Annotations
@Native
See Also
Socket#setSendBufferSize, Socket#getSendBufferSize, DatagramSocket#setSendBufferSize, DatagramSocket#getSendBufferSize
SO_TIMEOUTback to summary
public static final int SO_TIMEOUT

This option is used to both set and fetch a timeout value on blocking Socket operations:

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

Annotations
@Native
See Also
Socket#setSoTimeout, ServerSocket#setSoTimeout, DatagramSocket#setSoTimeout
TCP_NODELAYback to summary
public static final int TCP_NODELAY

See StandardSocketOptions#TCP_NODELAY for description of this socket option.

Annotations
@Native
See Also
Socket#setTcpNoDelay, Socket#getTcpNoDelay

Method Detail

getOptionback to summary
public Object getOption(int optID) throws SocketException

Fetch the value of an option. Binary options will return Boolean#TRUE if enabled, Boolean#FALSE if disabled, e.g.:

SocketImpl s; ... Boolean noDelay = (Boolean)(s.getOption(TCP_NODELAY)); if (noDelay.booleanValue()) { // true if TCP_NODELAY is enabled... ... }
SocketImpl s;
...
Boolean noDelay = (Boolean)(s.getOption(TCP_NODELAY));
if (noDelay.booleanValue()) {
    // true if TCP_NODELAY is enabled...
...
}

For options that take a particular type as a parameter, this method will return the parameter's value, else it will return Boolean#FALSE:

Object o = s.getOption(SO_LINGER); if (o instanceof Integer) { System.out.print("Linger time is " + ((Integer)o).intValue()); } else { // the true type of o is java.lang.Boolean.FALSE; }
Object o = s.getOption(SO_LINGER);
if (o instanceof Integer) {
    System.out.print("Linger time is " + ((Integer)o).intValue());
} else {
  // the true type of o is java.lang.Boolean.FALSE;
}
Parameters
optID:int

an int identifying the option to fetch

Returns:Object

the value of the option

Exceptions
SocketException:
if the socket is closed or if optID is unknown along the protocol stack
See Also
setOption(int, java.lang.Object)
setOptionback to summary
public void setOption(int optID, Object value) throws SocketException

Enable/disable the option specified by optID. If the option is to be enabled, and it takes an option-specific "value", this is passed in value. The actual type of value is option-specific, and it is an error to pass something that isn't of the expected type:

SocketImpl s; ... s.setOption(SO_LINGER, Integer.valueOf(10)); // OK - set SO_LINGER w/ timeout of 10 sec. s.setOption(SO_LINGER, Double.valueOf(10)); // ERROR - expects java.lang.Integer
SocketImpl s;
...
s.setOption(SO_LINGER, Integer.valueOf(10));
   // OK - set SO_LINGER w/ timeout of 10 sec.
s.setOption(SO_LINGER, Double.valueOf(10));
   // ERROR - expects java.lang.Integer
If the requested option is binary, it can be set using this method by a Boolean:
s.setOption(TCP_NODELAY, Boolean.TRUE); // OK - enables TCP_NODELAY, a binary option
s.setOption(TCP_NODELAY, Boolean.TRUE);
   // OK - enables TCP_NODELAY, a binary option
Any option can be disabled using this method with a Boolean#FALSE:
s.setOption(TCP_NODELAY, Boolean.FALSE); // OK - disables TCP_NODELAY s.setOption(SO_LINGER, Boolean.FALSE); // OK - disables SO_LINGER
s.setOption(TCP_NODELAY, Boolean.FALSE);
   // OK - disables TCP_NODELAY
s.setOption(SO_LINGER, Boolean.FALSE);
   // OK - disables SO_LINGER
For an option that has a notion of on and off, and requires a non-boolean parameter, setting its value to anything other than Boolean#FALSE implicitly enables it.
Parameters
optID:int

identifies the option

value:Object

the parameter of the socket option

Exceptions
SocketException:
if the option is unrecognized, the socket is closed, or some low-level error occurred
See Also
getOption(int)