java.lang.Object | |
↳ | java.net.Socket |
Known Direct Subclasses |
Provides a client-side TCP socket.
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Creates a new unconnected socket.
| |||||||||||
Creates a new unconnected socket using the given proxy type.
| |||||||||||
Creates a new streaming socket connected to the target host specified by
the parameters
dstName and dstPort . | |||||||||||
Creates a new streaming socket connected to the target host specified by
the parameters
dstName and dstPort . | |||||||||||
This constructor is deprecated.
Use
Socket(String, int) instead of this for streaming
sockets or an appropriate constructor of DatagramSocket for UDP transport.
| |||||||||||
Creates a new streaming socket connected to the target host specified by
the parameters
dstAddress and dstPort . | |||||||||||
Creates a new streaming socket connected to the target host specified by
the parameters
dstAddress and dstPort . | |||||||||||
This constructor is deprecated.
Use
Socket(InetAddress, int) instead of this for
streaming sockets or an appropriate constructor of DatagramSocket for UDP transport.
|
Protected Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Creates an unconnected socket with the given socket implementation.
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Binds this socket to the given local host address and port specified by
the SocketAddress
localAddr . | |||||||||||
Closes the socket.
| |||||||||||
Connects this socket to the given remote host address and port specified
by the SocketAddress
remoteAddr with the specified timeout. | |||||||||||
Connects this socket to the given remote host address and port specified
by the SocketAddress
remoteAddr . | |||||||||||
Gets the SocketChannel of this socket, if one is available.
| |||||||||||
Gets the IP address of the target host this socket is connected to.
| |||||||||||
Gets an input stream to read data from this socket.
| |||||||||||
Gets the setting of the socket option
SocketOptions.SO_KEEPALIVE . | |||||||||||
Gets the local IP address this socket is bound to.
| |||||||||||
Gets the local port this socket is bound to.
| |||||||||||
Gets the local address and port of this socket as a SocketAddress or
null if the socket is unbound. | |||||||||||
Gets the setting of the socket option
SocketOptions.SO_OOBINLINE . | |||||||||||
Gets an output stream to write data into this socket.
| |||||||||||
Gets the port number of the target host this socket is connected to.
| |||||||||||
Gets the receive buffer size of this socket.
| |||||||||||
Gets the remote address and port of this socket as a
SocketAddress or null if the socket is not connected. | |||||||||||
Gets the setting of the socket option
SocketOptions.SO_REUSEADDR . | |||||||||||
Gets the send buffer size of this socket.
| |||||||||||
Gets the value of the socket option
SocketOptions.SO_LINGER . | |||||||||||
Gets the timeout for this socket during which a reading operation shall
block while waiting for data.
| |||||||||||
Gets the setting of the socket option
SocketOptions.TCP_NODELAY . | |||||||||||
Gets the value of the socket option
SocketOptions.IP_TOS . | |||||||||||
Returns whether this socket is bound to a local address and port.
| |||||||||||
Returns whether this socket is closed.
| |||||||||||
Returns whether this socket is connected to a remote host.
| |||||||||||
Returns whether the incoming channel of the socket has already been
closed.
| |||||||||||
Returns whether the outgoing channel of the socket has already been
closed.
| |||||||||||
Sends the given single byte data which is represented by the lowest octet
of
value as "TCP urgent data". | |||||||||||
Sets the state of the
SocketOptions.SO_KEEPALIVE for this socket. | |||||||||||
Sets the state of the
SocketOptions.SO_OOBINLINE for this socket. | |||||||||||
Sets performance preferences for connectionTime, latency and bandwidth.
| |||||||||||
Sets the receive buffer size of this socket.
| |||||||||||
Sets the state of the
SocketOptions.SO_REUSEADDR for this socket. | |||||||||||
Sets the send buffer size of this socket.
| |||||||||||
Sets the state of the
SocketOptions.SO_LINGER with the given
timeout in seconds. | |||||||||||
Sets the reading timeout in milliseconds for this socket.
| |||||||||||
Sets the internal factory for creating socket implementations.
| |||||||||||
Sets the state of the
SocketOptions.TCP_NODELAY for this socket. | |||||||||||
Sets the value of the
SocketOptions.IP_TOS for this socket. | |||||||||||
Closes the input stream of this socket.
| |||||||||||
Closes the output stream of this socket.
| |||||||||||
Returns a
String containing a concise, human-readable description of the
socket. |
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class java.lang.Object
|
Creates a new unconnected socket. When a SocketImplFactory is defined it creates the internal socket implementation, otherwise the default socket implementation will be used for this socket.
Creates a new unconnected socket using the given proxy type. When a
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. |
---|---|
SecurityException | if a security manager exists and it denies the permission to connect to the given proxy. |
Creates a new streaming socket connected to the target host specified by
the parameters dstName
and dstPort
. The socket is bound
to any available port on the local host.
Implementation note: this implementation tries each
IP address for the given hostname until it either connects successfully
or it exhausts the set. It will try both IPv4 and IPv6 addresses in the
order specified by the system property "java.net.preferIPv6Addresses"
.
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. |
SecurityException | if a security manager exists and it denies the permission to connect to the given address and port. |
Creates a new streaming socket connected to the target host specified by
the parameters 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.
Implementation note: this implementation tries each
IP address for the given hostname until it either connects successfully
or it exhausts the set. It will try both IPv4 and IPv6 addresses in the
order specified by the system property "java.net.preferIPv6Addresses"
.
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. |
SecurityException | if a security manager exists and it denies the permission to connect to the given address and port. |
This constructor is deprecated.
Use Socket(String, int)
instead of this for streaming
sockets or an appropriate constructor of DatagramSocket
for UDP transport.
Creates a new streaming or datagram socket connected to the target host
specified by the parameters hostName
and port
. The socket
is bound to any available port on the local host.
Implementation note: this implementation tries each
IP address for the given hostname until it either connects successfully
or it exhausts the set. It will try both IPv4 and IPv6 addresses in the
order specified by the system property "java.net.preferIPv6Addresses"
.
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. |
SecurityException | if a security manager exists and it denies the permission to connect to the given address and port. |
Creates a new streaming socket connected to the target host specified by
the parameters dstAddress
and dstPort
. The socket is
bound to any available port on the local host.
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. |
---|---|
SecurityException | if a security manager exists and it denies the permission to connect to the given address and port. |
Creates a new streaming socket connected to the target host specified by
the parameters dstAddress
and dstPort
. On the local
endpoint the socket is bound to the given address localAddress
on
port localPort
.
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. |
localPort | the port on the local host to bind to. |
IOException | if an error occurs while creating the socket. |
---|---|
SecurityException | if a security manager exists and it denies the permission to connect to the given address and port. |
This constructor is deprecated.
Use Socket(InetAddress, int)
instead of this for
streaming sockets or an appropriate constructor of DatagramSocket
for UDP transport.
Creates a new streaming or datagram socket connected to the target host
specified by the parameters addr
and port
. The socket is
bound to any available port on the local host.
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. |
---|---|
SecurityException | if a security manager exists and it denies the permission to connect to the given address and port. |
Creates an unconnected socket with the given socket implementation.
anImpl | the socket implementation to be used. |
---|
SocketException | if an error occurs while creating the socket. |
---|
Binds this socket to the given local host address and port specified by
the SocketAddress 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. |
Closes the socket. It is not possible to reconnect or rebind to this socket thereafter which means a new socket instance has to be created.
IOException | if an error occurs while closing the socket. |
---|
Connects this socket to the given remote host address and port specified
by the SocketAddress 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. |
Connects this socket to the given remote host address and port specified
by the SocketAddress 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. |
Gets the SocketChannel of this socket, if one is available. The current
implementation of this method returns always null
.
null
if no channel exists.
Gets the IP address of the target host this socket is connected to.
null
if
this socket is not yet connected.
Gets an input stream to read data from this socket.
IOException | if an error occurs while creating the input stream or the socket is in an invalid state. |
---|
Gets the setting of the socket option SocketOptions.SO_KEEPALIVE
.
true
if the SocketOptions.SO_KEEPALIVE
is
enabled, false
otherwise.SocketException | if an error occurs while reading the socket option. |
---|
Gets the local IP address this socket is bound to.
InetAddress.ANY
if
the socket is unbound.
Gets the local port this socket is bound to.
-1
if the socket is
unbound.
Gets the local address and port of this socket as a SocketAddress or
null
if the socket is unbound. This is useful on multihomed
hosts.
Gets the setting of the socket option SocketOptions.SO_OOBINLINE
.
true
if the SocketOptions.SO_OOBINLINE
is
enabled, false
otherwise.SocketException | if an error occurs while reading the socket option. |
---|
Gets an output stream to write data into this socket.
IOException | if an error occurs while creating the output stream or the socket is in an invalid state. |
---|
Gets the port number of the target host this socket is connected to.
0
if this
socket is not yet connected.
Gets the receive buffer size of this socket.
SocketOptions.SO_RCVBUF
.SocketException | if an error occurs while reading the socket option. |
---|
Gets the remote address and port of this socket as a SocketAddress
or null
if the socket is not connected.
Gets the setting of the socket option SocketOptions.SO_REUSEADDR
.
true
if the SocketOptions.SO_REUSEADDR
is
enabled, false
otherwise.SocketException | if an error occurs while reading the socket option. |
---|
Gets the send buffer size of this socket.
SocketOptions.SO_SNDBUF
.SocketException | if an error occurs while reading the socket option. |
---|
Gets the value of the socket option SocketOptions.SO_LINGER
.
SocketOptions.SO_LINGER
or -1
if this option is disabled.SocketException | if an error occurs while reading the socket option. |
---|
Gets the timeout for this socket during which a reading operation shall block while waiting for data.
SocketOptions.SO_TIMEOUT
or 0
which represents an infinite timeout.SocketException | if an error occurs while reading the socket option. |
---|
Gets the setting of the socket option SocketOptions.TCP_NODELAY
.
true
if the SocketOptions.TCP_NODELAY
is enabled,
false
otherwise.SocketException | if an error occurs while reading the socket option. |
---|
Gets the value of the socket option SocketOptions.IP_TOS
.
SocketException | if an error occurs while reading the socket option. |
---|
Returns whether this socket is bound to a local address and port.
true
if the socket is bound to a local address, false
otherwise.
Returns whether this socket is closed.
true
if the socket is closed, false
otherwise.
Returns whether this socket is connected to a remote host.
true
if the socket is connected, false
otherwise.
Returns whether the incoming channel of the socket has already been closed.
true
if reading from this socket is not possible anymore,
false
otherwise.
Returns whether the outgoing channel of the socket has already been closed.
true
if writing to this socket is not possible anymore,
false
otherwise.
Sends the given single byte data which is represented by the lowest octet
of value
as "TCP urgent data".
value | the byte of urgent data to be sent. |
---|
IOException | if an error occurs while sending urgent data. |
---|
Sets the state of the SocketOptions.SO_KEEPALIVE
for this socket.
value | the state whether this option is enabled or not. |
---|
SocketException | if an error occurs while setting the option. |
---|
Sets the state of the SocketOptions.SO_OOBINLINE
for this socket.
When this option is enabled urgent data can be received in-line with
normal data.
oobinline | whether this option is enabled or not. |
---|
SocketException | if an error occurs while setting the option. |
---|
Sets performance preferences for connectionTime, latency and 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. |
Sets the receive buffer size of this socket.
size | the buffer size in bytes. This value must be a positive number
greater than 0 . |
---|
SocketException | if an error occurs while setting the size or the given value is an invalid size. |
---|
Sets the state of the SocketOptions.SO_REUSEADDR
for this socket.
reuse | the state whether this option is enabled or not. |
---|
SocketException | if an error occurs while setting the option. |
---|
Sets the send buffer size of this socket.
size | the buffer size in bytes. This value must be a positive number
greater than 0 . |
---|
SocketException | if an error occurs while setting the size or the given value is an invalid size. |
---|
Sets the state of the SocketOptions.SO_LINGER
with the given
timeout in seconds. The timeout value for this option is silently limited
to the maximum of 65535
.
on | the state whether this option is enabled or not. |
---|---|
timeout | the linger timeout value in seconds. |
SocketException | if an error occurs while setting the option. |
---|
Sets the reading timeout in milliseconds for this socket. The read
operation will block indefinitely if this option value is set to 0
. The timeout must be set before calling the read operation. A
SocketTimeoutException
is thrown when this timeout expires.
timeout | the reading timeout value as number greater than 0 or
0 for an infinite timeout. |
---|
SocketException | if an error occurs while setting the option. |
---|
Sets the internal factory for creating socket implementations. This may only be executed once during the lifetime of the application.
fac | the socket implementation factory to be set. |
---|
IOException | if the factory has been already set. |
---|
Sets the state of the SocketOptions.TCP_NODELAY
for this socket.
on | the state whether this option is enabled or not. |
---|
SocketException | if an error occurs while setting the option. |
---|
Sets the value of the SocketOptions.IP_TOS
for this socket. See
the specification RFC 1349 for more information about the type of service
field.
value | the value to be set for this option with a valid range of
0-255 . |
---|
SocketException | if an error occurs while setting the option. |
---|
Closes the input stream of this socket. Any further data sent to this
socket will be discarded. Reading from this socket after this method has
been called will return the value EOF
.
IOException | if an error occurs while closing the socket input stream. |
---|---|
SocketException | if the input stream is already closed. |
Closes the output stream of this socket. All buffered data will be sent
followed by the termination sequence. Writing to the closed output stream
will cause an IOException
.
IOException | if an error occurs while closing the socket output stream. |
---|---|
SocketException | if the output stream is already closed. |
Returns a String
containing a concise, human-readable description of the
socket.