public class Socket extends Object implements Closeable
| Modifier | Constructor and Description |
|---|---|
|
Socket()
Creates a new unconnected socket.
|
|
Socket(InetAddress dstAddress,
int dstPort)
Creates a new streaming socket connected to the target host specified by
the parameters
dstAddress and dstPort. |
|
Socket(InetAddress addr,
int port,
boolean streaming)
Deprecated.
Use
Socket(InetAddress, int) instead of this for
streaming sockets or an appropriate constructor of DatagramSocket for UDP transport. |
|
Socket(InetAddress dstAddress,
int dstPort,
InetAddress localAddress,
int localPort)
Creates a new streaming socket connected to the target host specified by
the parameters
dstAddress and dstPort. |
|
Socket(Proxy proxy)
Creates a new unconnected socket using the given proxy type.
|
protected |
Socket(SocketImpl impl)
Creates an unconnected socket with the given socket implementation.
|
|
Socket(String dstName,
int dstPort)
Creates a new streaming socket connected to the target host specified by
the parameters
dstName and dstPort. |
|
Socket(String hostName,
int port,
boolean streaming)
Deprecated.
Use
Socket(String, int) instead of this for streaming
sockets or an appropriate constructor of DatagramSocket for UDP transport. |
|
Socket(String dstName,
int dstPort,
InetAddress localAddress,
int localPort)
Creates a new streaming socket connected to the target host specified by
the parameters
dstName and dstPort. |
| Modifier and Type | Method and Description |
|---|---|
void |
bind(SocketAddress localAddr)
Binds this socket to the given local host address and port specified by
the SocketAddress
localAddr. |
void |
close()
Closes the socket.
|
void |
connect(SocketAddress remoteAddr)
Connects this socket to the given remote host address and port specified
by the SocketAddress
remoteAddr. |
void |
connect(SocketAddress remoteAddr,
int timeout)
Connects this socket to the given remote host address and port specified
by the SocketAddress
remoteAddr with the specified timeout. |
SocketChannel |
getChannel()
Returns this socket's
SocketChannel, if one exists. |
FileDescriptor |
getFileDescriptor$() |
InetAddress |
getInetAddress()
Returns the IP address of the target host this socket is connected to, or null if this
socket is not yet connected.
|
InputStream |
getInputStream()
Returns an input stream to read data from this socket.
|
boolean |
getKeepAlive()
Returns this socket's
SocketOptions.SO_KEEPALIVE setting. |
InetAddress |
getLocalAddress()
Returns the local IP address this socket is bound to, or an address for which
InetAddress.isAnyLocalAddress() returns true if the socket is closed or unbound. |
int |
getLocalPort()
Returns the local port this socket is bound to, or -1 if the socket is unbound.
|
SocketAddress |
getLocalSocketAddress()
Returns the local address and port of this socket as a SocketAddress or null if the socket
has never been bound.
|
boolean |
getOOBInline()
Returns this socket's
SocketOptions.SO_OOBINLINE setting. |
OutputStream |
getOutputStream()
Returns an output stream to write data into this socket.
|
int |
getPort()
Returns the port number of the target host this socket is connected to, or 0 if this socket
is not yet connected.
|
int |
getReceiveBufferSize()
Returns this socket's
receive buffer size. |
SocketAddress |
getRemoteSocketAddress()
Returns the remote address and port of this socket as a
SocketAddress or null if the socket is not connected. |
boolean |
getReuseAddress()
Returns this socket's
SocketOptions.SO_REUSEADDR setting. |
boolean |
getReusePort()
Returns this socket's
SocketOptions.SO_REUSEADDR setting. |
int |
getSendBufferSize()
Returns this socket's
send buffer size. |
int |
getSoLinger()
Returns this socket's
linger timeout in seconds, or -1
for no linger (i.e. |
int |
getSoTimeout()
Returns this socket's
receive timeout. |
boolean |
getTcpNoDelay()
Returns this socket's
SocketOptions#TCP_NODELAY setting. |
int |
getTrafficClass()
Returns this socket's
SocketOptions.IP_TOS setting. |
boolean |
isBound()
Returns whether this socket is bound to a local address and port.
|
boolean |
isClosed()
Returns whether this socket is closed.
|
boolean |
isConnected()
Returns whether this socket is connected to a remote host.
|
boolean |
isInputShutdown()
Returns whether the incoming channel of the socket has already been
closed.
|
boolean |
isOutputShutdown()
Returns whether the outgoing channel of the socket has already been
closed.
|
void |
onBind(InetAddress localAddress,
int localPort)
Sets the Socket and its related SocketImpl state as if a successful bind() took place,
without actually performing an OS bind().
|
void |
onClose()
Sets the Socket and its related SocketImpl state as if a successful close() took place,
without actually performing an OS close().
|
void |
onConnect(InetAddress remoteAddress,
int remotePort)
Sets the Socket and its related SocketImpl state as if a successful connect() took place,
without actually performing an OS connect().
|
void |
sendUrgentData(int value)
Sends the given single byte data which is represented by the lowest octet
of
value as "TCP urgent data". |
void |
setKeepAlive(boolean keepAlive)
Sets this socket's
SocketOptions.SO_KEEPALIVE option. |
void |
setOOBInline(boolean oobinline)
Sets this socket's
SocketOptions.SO_OOBINLINE option. |
void |
setPerformancePreferences(int connectionTime,
int latency,
int bandwidth)
Sets performance preferences for connectionTime, latency and bandwidth.
|
void |
setReceiveBufferSize(int size)
Sets this socket's
receive buffer size. |
void |
setReuseAddress(boolean reuse)
Sets this socket's
SocketOptions.SO_REUSEADDR option. |
void |
setReusePort(boolean reuse)
Sets this socket's
SocketOptions.SO_REUSEADDR option. |
void |
setSendBufferSize(int size)
Sets this socket's
send buffer size. |
static void |
setSocketImplFactory(SocketImplFactory fac)
Sets the internal factory for creating socket implementations.
|
void |
setSoLinger(boolean on,
int timeout)
Sets this socket's
linger timeout in seconds. |
void |
setSoTimeout(int timeout)
Sets this socket's
read timeout in milliseconds. |
void |
setTcpNoDelay(boolean on)
Sets this socket's
SocketOptions.TCP_NODELAY option. |
void |
setTrafficClass(int value)
Sets this socket's
SocketOptions.IP_TOS value for every packet sent by this socket. |
void |
shutdownInput()
Closes the input stream of this socket.
|
void |
shutdownOutput()
Closes the output stream of this socket.
|
String |
toString()
Returns a
String containing a concise, human-readable description of the
socket. |
public Socket()
SocketImplFactory,
SocketImplpublic Socket(Proxy proxy)
SocketImplFactory is defined it creates the internal socket
implementation, otherwise the default socket implementation will be used
for this socket.
Example that will create a socket connection through a SOCKS
proxy server:
Socket sock = new Socket(new Proxy(Proxy.Type.SOCKS, new
InetSocketAddress("test.domain.org", 2130)));
proxy - the specified proxy for this socket.IllegalArgumentException - if the argument proxy is null or of an
invalid type.SocketImplFactory,
SocketImplpublic Socket(String dstName, int dstPort) throws UnknownHostException, IOException
dstName and dstPort. The socket is bound
to any available port on the local host.
This implementation tries each IP address for the given hostname (in RFC 3484 order) until it either connects successfully or it exhausts the set.
dstName - the target host name or IP address to connect to.dstPort - the port on the target host to connect to.UnknownHostException - if the host name could not be resolved into an IP address.IOException - if an error occurs while creating the socket.public Socket(String dstName, int dstPort, InetAddress localAddress, int localPort) throws IOException
dstName and dstPort. On the local endpoint
the socket is bound to the given address localAddress on port
localPort. If host is null a loopback address is used to connect to.
This implementation tries each IP address for the given hostname (in RFC 3484 order) until it either connects successfully or it exhausts the set.
dstName - the target host name or IP address to connect to.dstPort - the port on the target host to connect to.localAddress - the address on the local host to bind to.localPort - the port on the local host to bind to.UnknownHostException - if the host name could not be resolved into an IP address.IOException - if an error occurs while creating the socket.@Deprecated public Socket(String hostName, int port, boolean streaming) throws IOException
Socket(String, int) instead of this for streaming
sockets or an appropriate constructor of DatagramSocket for UDP transport.hostName and port. The socket
is bound to any available port on the local host.
This implementation tries each IP address for the given hostname (in RFC 3484 order) until it either connects successfully or it exhausts the set.
hostName - the target host name or IP address to connect to.port - the port on the target host to connect to.streaming - if true a streaming socket is returned, a datagram
socket otherwise.UnknownHostException - if the host name could not be resolved into an IP address.IOException - if an error occurs while creating the socket.public Socket(InetAddress dstAddress, int dstPort) throws IOException
dstAddress and dstPort. The socket is
bound to any available port on the local host using a wildcard address.dstAddress - the target host address to connect to.dstPort - the port on the target host to connect to.IOException - if an error occurs while creating the socket.public Socket(InetAddress dstAddress, int dstPort, InetAddress localAddress, int localPort) throws IOException
dstAddress and dstPort.
On the local endpoint the socket is bound to the given address localAddress on
port localPort. If localAddress is null the socket will be bound to a
wildcard address.
dstAddress - the target host address to connect to.dstPort - the port on the target host to connect to.localAddress - the address on the local host to bind to, or null.localPort - the port on the local host to bind to.IOException - if an error occurs while creating the socket.@Deprecated public Socket(InetAddress addr, int port, boolean streaming) throws IOException
Socket(InetAddress, int) instead of this for
streaming sockets or an appropriate constructor of DatagramSocket for UDP transport.addr and port. The socket is
bound to any available port on the local host using a wildcard address.addr - the Internet address to connect to.port - the port on the target host to connect to.streaming - if true a streaming socket is returned, a datagram
socket otherwise.IOException - if an error occurs while creating the socket.protected Socket(SocketImpl impl) throws SocketException
impl - the socket implementation to be used.SocketException - if an error occurs while creating the socket.public void close()
throws IOException
close in interface Closeableclose in interface AutoCloseableIOException - if an error occurs while closing the socket.public void onClose()
public InetAddress getInetAddress()
public InputStream getInputStream() throws IOException
SocketChannel and that channel is in non-blocking mode then reads from the
stream will throw a IllegalBlockingModeException.IOException - if an error occurs while creating the input stream or the
socket is in an invalid state.public boolean getKeepAlive()
throws SocketException
SocketOptions.SO_KEEPALIVE setting.SocketExceptionpublic InetAddress getLocalAddress()
InetAddress.isAnyLocalAddress() returns true if the socket is closed or unbound.public int getLocalPort()
public OutputStream getOutputStream() throws IOException
SocketChannel and that channel is in non-blocking mode then writes to the
stream will throw a IllegalBlockingModeException.IOException - if an error occurs while creating the output stream or the
socket is in an invalid state.public int getPort()
public int getSoLinger()
throws SocketException
linger timeout in seconds, or -1
for no linger (i.e. close will return immediately).SocketExceptionpublic int getReceiveBufferSize()
throws SocketException
receive buffer size.SocketExceptionpublic int getSendBufferSize()
throws SocketException
send buffer size.SocketExceptionpublic int getSoTimeout()
throws SocketException
receive timeout.SocketExceptionpublic boolean getTcpNoDelay()
throws SocketException
SocketOptions#TCP_NODELAY setting.SocketExceptionpublic void setKeepAlive(boolean keepAlive)
throws SocketException
SocketOptions.SO_KEEPALIVE option.SocketExceptionpublic static void setSocketImplFactory(SocketImplFactory fac) throws IOException
fac - the socket implementation factory to be set.IOException - if the factory has been already set.public void setSendBufferSize(int size)
throws SocketException
send buffer size.SocketExceptionpublic void setReceiveBufferSize(int size)
throws SocketException
receive buffer size.SocketExceptionpublic void setSoLinger(boolean on,
int timeout)
throws SocketException
SocketExceptionpublic void setSoTimeout(int timeout)
throws SocketException
read timeout in milliseconds.
Use 0 for no timeout.
To take effect, this option must be set before the blocking method was called.SocketExceptionpublic void setTcpNoDelay(boolean on)
throws SocketException
SocketOptions.TCP_NODELAY option.SocketExceptionpublic String toString()
String containing a concise, human-readable description of the
socket.public void shutdownInput()
throws IOException
EOF.IOException - if an error occurs while closing the socket input stream.SocketException - if the input stream is already closed.public void shutdownOutput()
throws IOException
IOException.IOException - if an error occurs while closing the socket output stream.SocketException - if the output stream is already closed.public SocketAddress getLocalSocketAddress()
InetAddress.isAnyLocalAddress() returns true will be returned with the
previously-bound port. This is useful on multihomed hosts.public SocketAddress getRemoteSocketAddress()
SocketAddress or null if the socket is not connected.public boolean isBound()
true if the socket is bound to a local address, false otherwise.public boolean isConnected()
true if the socket is connected, false otherwise.public boolean isClosed()
true if the socket is closed, false otherwise.public void bind(SocketAddress localAddr) throws IOException
localAddr. If localAddr is set to
null, this socket will be bound to an available local address on
any free port.localAddr - the specific address and port on the local machine to bind to.IllegalArgumentException - if the given SocketAddress is invalid or not supported.IOException - if the socket is already bound or an error occurs while
binding.public void onBind(InetAddress localAddress, int localPort)
public void connect(SocketAddress remoteAddr) throws IOException
remoteAddr.remoteAddr - the address and port of the remote host to connect to.IllegalArgumentException - if the given SocketAddress is invalid or not supported.IOException - if the socket is already connected or an error occurs while
connecting.public void connect(SocketAddress remoteAddr, int timeout) throws IOException
remoteAddr with the specified timeout. The
connecting method will block until the connection is established or an
error occurred.remoteAddr - the address and port of the remote host to connect to.timeout - the timeout value in milliseconds or 0 for an infinite
timeout.IllegalArgumentException - if the given SocketAddress is invalid or not supported or the
timeout value is negative.IOException - if the socket is already connected or an error occurs while
connecting.public void onConnect(InetAddress remoteAddress, int remotePort)
public boolean isInputShutdown()
true if reading from this socket is not possible anymore,
false otherwise.public boolean isOutputShutdown()
true if writing to this socket is not possible anymore,
false otherwise.public void setReuseAddress(boolean reuse)
throws SocketException
SocketOptions.SO_REUSEADDR option.SocketExceptionpublic boolean getReuseAddress()
throws SocketException
SocketOptions.SO_REUSEADDR setting.SocketExceptionpublic void setReusePort(boolean reuse)
throws SocketException
SocketOptions.SO_REUSEADDR option.SocketExceptionpublic boolean getReusePort()
throws SocketException
SocketOptions.SO_REUSEADDR setting.SocketExceptionpublic void setOOBInline(boolean oobinline)
throws SocketException
SocketOptions.SO_OOBINLINE option.SocketExceptionpublic boolean getOOBInline()
throws SocketException
SocketOptions.SO_OOBINLINE setting.SocketExceptionpublic void setTrafficClass(int value)
throws SocketException
SocketOptions.IP_TOS value for every packet sent by this socket.SocketExceptionpublic int getTrafficClass()
throws SocketException
SocketOptions.IP_TOS setting.SocketExceptionpublic void sendUrgentData(int value)
throws IOException
value as "TCP urgent data".value - the byte of urgent data to be sent.IOException - if an error occurs while sending urgent data.public SocketChannel getChannel()
SocketChannel, if one exists. A channel is
available only if this socket wraps a channel. (That is, you can go from a
channel to a socket and back again, but you can't go from an arbitrary socket to a channel.)
In practice, this means that the socket must have been created by
ServerSocketChannel.accept() or
SocketChannel.open().public FileDescriptor getFileDescriptor$()
public void setPerformancePreferences(int connectionTime,
int latency,
int bandwidth)
This method does currently nothing.
connectionTime - the value representing the importance of a short connecting
time.latency - the value representing the importance of low latency.bandwidth - the value representing the importance of high bandwidth.