Top Description Inners Fields Constructors Methods
java.net

public Class Socket

extends Object
implements Closeable
Class Inheritance
All Implemented Interfaces
java.io.Closeable, java.lang.AutoCloseable
Known Direct Subclasses
javax.net.ssl.SSLSocket, sun.nio.ch.SocketAdaptor
Imports
jdk.internal.event.SocketReadEvent, .SocketWriteEvent, sun.security.util.SecurityConstants, java.io.InputStream, .InterruptedIOException, .OutputStream, .IOException, java.lang.invoke.MethodHandles, .VarHandle, java.nio.channels.ClosedByInterruptException, .SocketChannel, java.util.Objects, .Set, .Collections

This class implements client sockets (also called just "sockets"). A socket is an endpoint for communication between two machines.

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:

Socket options
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
Additional (implementation specific) options may also be supported.
Since
1.0
See Also
java.net.SocketImpl, java.nio.channels.SocketChannel

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
Socket.SocketInputStream

An InputStream that delegates read/available operations to an underlying input stream.

private static class
Socket.SocketOutputStream

An OutputStream that delegates write operations to an underlying output stream.

Field Summary

Modifier and TypeField 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<SocketOption<?>>
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

Constructor Summary

AccessConstructor and Description
private
Socket(Void unused, SocketImpl impl)

Creates an unconnected socket with the given SocketImpl.

public
Socket()

Creates an unconnected Socket.

public
Socket(Proxy
a Proxy object specifying what kind of proxying should be used.
proxy
)

Creates an unconnected socket, specifying the type of proxy, if any, that should be used regardless of any other settings.

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
Socket(String
the host name, or null for the loopback address.
host
,
int
the port number.
port
)

Creates a stream socket and connects it to the specified port number on the named host.

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 null for the loopback address.
host
,
int
the remote port
port
,
InetAddress
the local address the socket is bound to, or null for the anyLocal address.
localAddr
,
int
the local port the socket is bound to, or zero for a system selected free port.
localPort
)

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 null for the anyLocal address.
localAddr
,
int
the local port the socket is bound to or zero for a system selected free port.
localPort
)

Creates a socket and connects it to the specified remote address on the specified remote port.

public
Socket(String
the host name, or null for the loopback address.
host
,
int
the port number.
port
,
boolean
a boolean indicating whether this is a stream socket or a datagram socket.
stream
)
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 true, create a stream socket; otherwise, create a datagram socket.
stream
)
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.

Method Summary

Modifier and TypeMethod and Description
public void
bind(SocketAddress
the SocketAddress to bind to
bindpoint
)

Binds the socket to a local address.

private void
private static Void
public void
close()

Implements java.io.Closeable.close.

Closes this socket.

public void
connect(SocketAddress
the SocketAddress
endpoint
)

Connects this socket to the server.

public void
connect(SocketAddress
the SocketAddress
endpoint
,
int
the timeout value to be used in milliseconds.
timeout
)

Connects this socket to the server with a specified timeout value.

private static SocketImpl
createImpl()

Create a new SocketImpl for a connecting/client socket.

private int

Returns:

the previous state value
getAndBitwiseOrState
(int mask)

Atomically 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, or null if this socket was not created for a channel
getChannel
()

Returns the unique SocketChannel object associated with this socket, if any.

private SocketImpl
getImpl()

Returns the SocketImpl for this Socket, creating it, and the underlying socket, if required.

public InetAddress

Returns:

the remote IP address to which this socket is connected, or null if the socket is not connected.
getInetAddress
()

Returns the address to which the socket is connected.

public InputStream

Returns:

an input stream for reading bytes from this socket.
getInputStream
()

Returns an input stream for this socket.

public boolean

Returns:

a boolean indicating whether or not SO_KEEPALIVE is enabled.
getKeepAlive
()

Tests if SO_KEEPALIVE is enabled.

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.
getLocalAddress
()

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.
getLocalPort
()

Returns the local port number to which this socket is bound.

public SocketAddress

Returns:

a SocketAddress 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.
getLocalSocketAddress
()

Returns the address of the endpoint this socket is bound to.

public boolean

Returns:

a boolean indicating whether or not SO_OOBINLINE is enabled.
getOOBInline
()

Tests if SO_OOBINLINE is enabled.

public <
The type of the socket option value
T
>
T

Returns:

The value of the socket option.
getOption
(SocketOption<T>
The socket option
name
)

Returns the value of a socket option.

public OutputStream

Returns:

an output stream for writing bytes to this socket.
getOutputStream
()

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.
getPort
()

Returns the remote port number to which this socket is connected.

public int

Returns:

the value of the SO_RCVBUF option for this Socket.
getReceiveBufferSize
()

Gets the value of the SO_RCVBUF option for this Socket, that is the buffer size used by the platform for input on this Socket.

public SocketAddress

Returns:

a SocketAddress representing the remote endpoint of this socket, or null if it is not connected yet.
getRemoteSocketAddress
()

Returns the address of the endpoint this socket is connected to, or null if it is unconnected.

public boolean

Returns:

a boolean indicating whether or not SO_REUSEADDR is enabled.
getReuseAddress
()

Tests if SO_REUSEADDR is enabled.

public int

Returns:

the value of the SO_SNDBUF option for this Socket.
getSendBufferSize
()

Get value of the SO_SNDBUF option for this Socket, that is the buffer size used by the platform for output on this Socket.

public int

Returns:

the setting for SO_LINGER.
getSoLinger
()

Returns setting for SO_LINGER.

public int

Returns:

the setting for SO_TIMEOUT
getSoTimeout
()

Returns setting for SO_TIMEOUT.

public boolean

Returns:

a boolean indicating whether or not TCP_NODELAY is enabled.
getTcpNoDelay
()

Tests if TCP_NODELAY is enabled.

public int

Returns:

the traffic class or type-of-service already set
getTrafficClass
()

Gets 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 setTrafficClass(int) this method may return a different value than was previously set using the setTrafficClass(int) method on this Socket.

pack-priv SocketImpl
impl()

Returns the SocketImpl, may be null.

private static boolean
isBound(int s)

public boolean

Returns:

true if the socket was successfully bound to an address
isBound
()

Returns the binding state of the socket.

private static boolean
isClosed(int s)

public boolean

Returns:

true if the socket has been closed
isClosed
()

Returns the closed state of the socket.

private static boolean
isConnected(int s)

public boolean

Returns:

true if the socket was successfully connected to a server
isConnected
()

Returns the connection state of the socket.

private static boolean
public boolean

Returns:

true if the input of the socket has been shutdown
isInputShutdown
()

Returns 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 shutdown
isOutputShutdown
()

Returns whether the write-half of the socket connection is closed.

public void
sendUrgentData(int
The byte of data to send
data
)

Send one byte of urgent data on the socket.

pack-priv void
setConnected()

Sets to Socket state for a newly accepted connection.

pack-priv void
setConnectedImpl(SocketImpl si)

Sets the SocketImpl.

pack-priv void
setImpl(SocketImpl si)

Sets the SocketImpl.

public void
setKeepAlive(boolean
whether or not to have socket keep alive turned on.
on
)

Enable/disable SO_KEEPALIVE.

public void
setOOBInline(boolean
true to enable SO_OOBINLINE, false to disable.
on
)

Enable/disable SO_OOBINLINE (receipt of TCP urgent data) By default, this option is disabled and TCP urgent data received on a socket is silently discarded.

public <
The type of the socket option value
T
>
Socket

Returns:

this Socket
setOption
(SocketOption<T>
The socket option
name
,
T
The value of the socket option. A value of null may be valid for some options.
value
)

Sets the value of a socket option.

public void
setPerformancePreferences(int
An int expressing the relative importance of a short connection time
connectionTime
,
int
An int expressing the relative importance of low latency
latency
,
int
An int expressing the relative importance of high bandwidth
bandwidth
)

Sets 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 SO_RCVBUF option to the specified value for this Socket.

public void
setReuseAddress(boolean
whether to enable or disable the socket option
on
)

Enable/disable the SO_REUSEADDR socket option.

public void
setSendBufferSize(int
the size to which to set the send buffer size. This value must be greater than 0.
size
)

Sets the SO_SNDBUF option to the specified value for this Socket.

public static synchronized void
setSocketImplFactory(SocketImplFactory
the desired factory.
fac
)
Deprecated since 17. Use a javax.net.SocketFactory 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 SO_LINGER with the specified linger time in seconds.

public void
setSoTimeout(int
the specified timeout, in milliseconds.
timeout
)

Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds.

public void
setTcpNoDelay(boolean
true to enable TCP_NODELAY, false to disable.
on
)

Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm).

public void
setTrafficClass(int
an int value for the bitset.
tc
)

Sets traffic class or type-of-service octet in the IP header for packets sent from this Socket.

public void
shutdownInput()

Places the input stream for this socket at "end of stream".

public void
shutdownOutput()

Disables the output stream for this socket.

pack-priv static SocketImplFactory
public Set<SocketOption<?>>

Returns:

A set of the socket options supported by this socket. This set may be empty if the socket's SocketImpl cannot be created.
supportedOptions
()

Returns a set of the socket options supported by this socket.

public String

Returns:

a string representation of this socket.
toString
()

Overrides java.lang.Object.toString.

Converts this socket to a String.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait