The actual work of the socket is performed by an instance of the
SocketImpl
class.
The Socket
class defines convenience
methods to set and get several socket options. This class also
defines the setOption
and getOption
methods to set
and query socket options.
A Socket
support the following options:
Additional (implementation specific) options may also be supported.
Option Name Description SO_SNDBUF
The size of the socket send buffer SO_RCVBUF
The size of the socket receive buffer SO_KEEPALIVE
Keep connection alive SO_REUSEADDR
Re-use address SO_LINGER
Linger on close if data is present (when configured in blocking mode only) TCP_NODELAY
Disable the Nagle algorithm
java.net.SocketImpl
, java.nio.channels.SocketChannel
Modifier and Type | Class and Description |
---|---|
private static class | Socket.
An InputStream that delegates read/available operations to an underlying input stream. |
private static class | Socket.
An OutputStream that delegates write operations to an underlying output stream. |
Modifier and Type | Field and Description |
---|---|
private static final int | |
private static final int | |
private static final int | |
private static volatile SocketImplFactory | factory
The factory for all client sockets. |
private volatile SocketImpl | |
private static final VarHandle | |
private volatile InputStream | |
private volatile Set | |
private static final VarHandle | |
private volatile OutputStream | |
private static final int | |
private static final int | |
private static final int | |
private final Object | |
private static final VarHandle | |
private volatile int |
Access | Constructor and Description |
---|---|
private | |
public | |
public | |
protected | Socket(SocketImpl
an instance of a SocketImpl
the subclass wishes to use on the Socket. impl)Creates an unconnected Socket with a user-specified SocketImpl. |
public | |
public | Socket(InetAddress
the IP address. address, int the port number. port)Creates a stream socket and connects it to the specified port number at the specified IP address. |
public | Socket(String
the name of the remote host, or host, int null for the loopback address.the remote port port, InetAddress the local address the socket is bound to, or
localAddr, int null for the anyLocal address.the local port the socket is bound to, or
localPort)zero for a system selected free port.Creates a socket and connects it to the specified remote host on the specified remote port. |
public | Socket(InetAddress
the remote address address, int the remote port port, InetAddress the local address the socket is bound to, or
localAddr, int null for the anyLocal address.the local port the socket is bound to or
localPort)zero for a system selected free port.Creates a socket and connects it to the specified remote address on the specified remote port. |
public | Socket(String
the host name, or host, int null for the loopback address.the port number. port, boolean a stream)boolean indicating whether this is
a stream socket or a datagram socket.
Deprecated
for removal since 1.1. Use
DatagramSocket instead for UDP transport.
Creates a stream socket and connects it to the specified port number on the named host. |
public | Socket(InetAddress
the IP address. host, int the port number. port, boolean if stream)true , create a stream socket;
otherwise, create a datagram socket.
Deprecated
for removal since 1.1. Use
DatagramSocket instead for UDP transport.
Creates a socket and connects it to the specified port number at the specified IP address. |
private | Socket(SocketAddress
the remote address to connect to address, SocketAddress the local address to bind to, can be null localAddr, boolean true for a stream socket, false for a datagram socket stream)Initialize a new Socket that is connected to the given remote address. |
Modifier and Type | Method and Description |
---|---|
public void | |
private void | |
private static Void | |
public void | |
public void | |
public void | connect(SocketAddress
the endpoint, int SocketAddress the timeout value to be used in milliseconds. timeout)Connects this socket to the server with a specified timeout value. |
private static SocketImpl | |
private int | Returns: the previous state valueAtomically sets state to the result of a bitwise OR of the current value and the given mask. |
public SocketChannel | Returns: the socket channel associated with this socket, ornull if this socket was not created
for a channelReturns the unique |
private SocketImpl | getImpl()
Returns the |
public InetAddress | Returns: the remote IP address to which this socket is connected, ornull if the socket is not connected.Returns the address to which the socket is connected. |
public InputStream | Returns: an input stream for reading bytes from this socket.Returns an input stream for this socket. |
public boolean | Returns: aboolean indicating whether or not
SO_KEEPALIVE is enabled.Tests if |
public InetAddress | Returns: the local address to which the socket is bound, the loopback address if denied by the security manager, or the wildcard address if the socket is closed or not bound yet.Gets the local address to which the socket is bound. |
public int | Returns: the local port number to which this socket is bound or -1 if the socket is not bound yet.Returns the local port number to which this socket is bound. |
public SocketAddress | Returns: aSocketAddress representing the local endpoint of
this socket, or a SocketAddress representing the
loopback address if denied by the security manager, or
null if the socket is not bound yet.Returns the address of the endpoint this socket is bound to. |
public boolean | Returns: aboolean indicating whether or not
SO_OOBINLINE is enabled.Tests if |
public < The type of the socket option value T> T | Returns: The value of the socket option.The socket option name)Returns the value of a socket option. |
public OutputStream | Returns: an output stream for writing bytes to this socket.Returns an output stream for this socket. |
public int | Returns: the remote port number to which this socket is connected, or 0 if the socket is not connected yet.Returns the remote port number to which this socket is connected. |
public int | Returns: the value of theSO_RCVBUF option for this Socket .Gets the value of the |
public SocketAddress | Returns: aSocketAddress representing the remote endpoint of this
socket, or null if it is not connected yet.Returns the address of the endpoint this socket is connected to, or
|
public boolean | Returns: aboolean indicating whether or not
SO_REUSEADDR is enabled.Tests if |
public int | Returns: the value of theSO_SNDBUF option for this Socket .Get value of the |
public int | |
public int | |
public boolean | Returns: aboolean indicating whether or not
TCP_NODELAY is enabled.Tests if |
public int | Returns: the traffic class or type-of-service already setGets traffic class or type-of-service in the IP header for packets sent from this Socket
As the underlying network implementation may ignore the
traffic class or type-of-service set using |
pack-priv SocketImpl | |
private static boolean | |
public boolean | Returns: true if the socket was successfully bound to an addressReturns the binding state of the socket. |
private static boolean | |
public boolean | |
private static boolean | |
public boolean | Returns: true if the socket was successfully connected to a serverReturns the connection state of the socket. |
private static boolean | |
public boolean | Returns: true if the input of the socket has been shutdownReturns whether the read-half of the socket connection is closed. |
private static boolean | |
public boolean | Returns: true if the output of the socket has been shutdownReturns whether the write-half of the socket connection is closed. |
public void | |
pack-priv void | |
pack-priv void | |
pack-priv void | |
public void | setKeepAlive(boolean
whether or not to have socket keep alive turned on. on)Enable/disable |
public void | setOOBInline(boolean
true to enable SO_OOBINLINE ,
false to disable.Enable/disable |
public < The type of the socket option value T> Socket | Returns: this SocketThe socket option name, T The value of the socket option. A value of value)null
may be valid for some options.Sets the value of a socket option. |
public void | setPerformancePreferences(int
An connectionTime, int int expressing the relative importance of a short
connection timeAn latency, int int expressing the relative importance of low
latencyAn bandwidth)int expressing the relative importance of high
bandwidthSets performance preferences for this socket. |
public void | setReceiveBufferSize(int
the size to which to set the receive buffer
size. This value must be greater than 0. size)Sets the |
public void | setReuseAddress(boolean
whether to enable or disable the socket option on)Enable/disable the |
public void | setSendBufferSize(int
the size to which to set the send buffer
size. This value must be greater than 0. size)Sets the |
public static synchronized void | setSocketImplFactory(SocketImplFactory
the desired factory. fac)
Deprecated
since 17. Use a
javax. and subclass Socket
directly.
Sets the client socket implementation factory for the application. |
public void | setSoLinger(boolean
whether or not to linger on. on, int how long to linger for, if on is true. linger)Enable/disable |
public void | setSoTimeout(int
the specified timeout, in milliseconds. timeout)Enable/disable |
public void | setTcpNoDelay(boolean
true to enable TCP_NODELAY ,
false to disable.Enable/disable |
public void | setTrafficClass(int
an tc)int value for the bitset.Sets traffic class or type-of-service octet in the IP header for packets sent from this Socket. |
public void | |
public void | |
pack-priv static SocketImplFactory | |
public Set | Returns: A set of the socket options supported by this socket. This set may be empty if the socket's SocketImpl cannot be created.Returns a set of the socket options supported by this socket. |
public String | Returns: a string representation of this socket.Overrides java. Converts this socket to a |