GeezerVison is showing docs.oracle.com — Socket (Java Platform SE 8 )
Java™ Platform Standard Ed. 8
compact1, compact2, compact3
java.net
Class Socket
-
-
Constructor Summary
Constructors
| Modifier |
Constructor |
Description |
|
Socket() |
Creates an unconnected socket, with the
system-default type of SocketImpl.
|
|
Socket(InetAddress address,
int port) |
Creates a stream socket and connects it to the specified port
number at the specified IP address.
|
|
Socket(InetAddress host,
int port,
boolean stream) |
Deprecated.
Use DatagramSocket instead for UDP transport.
|
|
Socket(InetAddress address,
int port,
InetAddress localAddr,
int localPort) |
Creates a socket and connects it to the specified remote address on
the specified remote port.
|
|
Socket(Proxy proxy) |
Creates an unconnected socket, specifying the type of proxy, if any,
that should be used regardless of any other settings.
|
protected |
Socket(SocketImpl impl) |
Creates an unconnected Socket with a user-specified
SocketImpl.
|
|
Socket(String host,
int port) |
Creates a stream socket and connects it to the specified port
number on the named host.
|
|
Socket(String host,
int port,
boolean stream) |
Deprecated.
Use DatagramSocket instead for UDP transport.
|
|
Socket(String host,
int port,
InetAddress localAddr,
int localPort) |
Creates a socket and connects it to the specified remote host on
the specified remote port.
|
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods
| Modifier and Type |
Method |
Description |
void |
bind(SocketAddress bindpoint) |
Binds the socket to a local address.
|
void |
close() |
Closes this socket.
|
void |
connect(SocketAddress endpoint) |
Connects this socket to the server.
|
void |
connect(SocketAddress endpoint,
int timeout) |
Connects this socket to the server with a specified timeout value.
|
SocketChannel |
getChannel() |
Returns the unique SocketChannel
object associated with this socket, if any.
|
InetAddress |
getInetAddress() |
Returns the address to which the socket is connected.
|
InputStream |
getInputStream() |
Returns an input stream for this socket.
|
boolean |
getKeepAlive() |
|
InetAddress |
getLocalAddress() |
Gets the local address to which the socket is bound.
|
int |
getLocalPort() |
Returns the local port number to which this socket is bound.
|
SocketAddress |
getLocalSocketAddress() |
Returns the address of the endpoint this socket is bound to.
|
boolean |
getOOBInline() |
|
OutputStream |
getOutputStream() |
Returns an output stream for this socket.
|
int |
getPort() |
Returns the remote port number to which this socket is connected.
|
int |
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.
|
SocketAddress |
getRemoteSocketAddress() |
Returns the address of the endpoint this socket is connected to, or
null if it is unconnected.
|
boolean |
getReuseAddress() |
|
int |
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.
|
int |
getSoLinger() |
|
int |
getSoTimeout() |
|
boolean |
getTcpNoDelay() |
|
int |
getTrafficClass() |
Gets traffic class or type-of-service in the IP header
for packets sent from this Socket
|
boolean |
isBound() |
Returns the binding state of the socket.
|
boolean |
isClosed() |
Returns the closed state of the socket.
|
boolean |
isConnected() |
Returns the connection state of the socket.
|
boolean |
isInputShutdown() |
Returns whether the read-half of the socket connection is closed.
|
boolean |
isOutputShutdown() |
Returns whether the write-half of the socket connection is closed.
|
void |
sendUrgentData(int data) |
Send one byte of urgent data on the socket.
|
void |
setKeepAlive(boolean on) |
|
void |
setOOBInline(boolean 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.
|
void |
setPerformancePreferences(int connectionTime,
int latency,
int bandwidth) |
Sets performance preferences for this socket.
|
void |
setReceiveBufferSize(int size) |
Sets the SO_RCVBUF option to the
specified value for this Socket.
|
void |
setReuseAddress(boolean on) |
|
void |
setSendBufferSize(int size) |
Sets the SO_SNDBUF option to the
specified value for this Socket.
|
static void |
setSocketImplFactory(SocketImplFactory fac) |
Sets the client socket implementation factory for the
application.
|
void |
setSoLinger(boolean on,
int linger) |
Enable/disable SO_LINGER with the
specified linger time in seconds.
|
void |
setSoTimeout(int timeout) |
Enable/disable SO_TIMEOUT
with the specified timeout, in milliseconds.
|
void |
setTcpNoDelay(boolean on) |
Enable/disable TCP_NODELAY
(disable/enable Nagle's algorithm).
|
void |
setTrafficClass(int tc) |
Sets traffic class or type-of-service octet in the IP
header for packets sent from this Socket.
|
void |
shutdownInput() |
Places the input stream for this socket at "end of stream".
|
void |
shutdownOutput() |
Disables the output stream for this socket.
|
String |
toString() |
Converts this socket to a String.
|
-
-
Constructor Detail
-
Socket
public Socket(InetAddress address,
int port,
InetAddress localAddr,
int localPort)
throws IOException
Creates a socket and connects it to the specified remote address on
the specified remote port. The Socket will also bind() to the local
address and port supplied.
If the specified local address is null it is the equivalent of
specifying the address as the AnyLocal address
(see InetAddress.isAnyLocalAddress()).
A local port number of zero will let the system pick up a
free port in the bind operation.
If there is a security manager, its
checkConnect method is called
with the host address and port
as its arguments. This could result in a SecurityException.
- Parameters:
address - the remote address
port - the remote port
localAddr - the local address the socket is bound to, or
null for the anyLocal address.
localPort - the local port the socket is bound to or
zero for a system selected free port.
- Throws:
IOException - if an I/O error occurs when creating the socket.
SecurityException - if a security manager exists and its
checkConnect method doesn't allow the connection
to the destination, or if its checkListen method
doesn't allow the bind to the local port.
IllegalArgumentException - if the port parameter or localPort
parameter is outside the specified range of valid port values,
which is between 0 and 65535, inclusive.
NullPointerException - if address is null.
- Since:
- JDK1.1
- See Also:
SecurityManager.checkConnect(java.lang.String, int)
-
Socket
@Deprecated
public Socket(String host,
int port,
boolean stream)
throws IOException
Deprecated. Use DatagramSocket instead for UDP transport.
Creates a stream socket and connects it to the specified port
number on the named host.
If the specified host is null it is the equivalent of
specifying the address as
InetAddress.getByName(null).
In other words, it is equivalent to specifying an address of the
loopback interface.
If the stream argument is true, this creates a
stream socket. If the stream argument is false, it
creates a datagram socket.
If the application has specified a server socket factory, that
factory's createSocketImpl method is called to create
the actual socket implementation. Otherwise a "plain" socket is created.
If there is a security manager, its
checkConnect method is called
with the host address and port
as its arguments. This could result in a SecurityException.
If a UDP socket is used, TCP/IP related socket options will not apply.
- Parameters:
host - the host name, or null for the loopback address.
port - the port number.
stream - a boolean indicating whether this is
a stream socket or a datagram socket.
- Throws:
IOException - if an I/O error occurs when creating the socket.
SecurityException - if a security manager exists and its
checkConnect method doesn't allow the operation.
IllegalArgumentException - if the port parameter is outside
the specified range of valid port values, which is between
0 and 65535, inclusive.
- See Also:
setSocketImplFactory(java.net.SocketImplFactory),
SocketImpl,
SocketImplFactory.createSocketImpl(),
SecurityManager.checkConnect(java.lang.String, int)
-
Socket
@Deprecated
public Socket(InetAddress host,
int port,
boolean stream)
throws IOException
Deprecated. Use DatagramSocket instead for UDP transport.
Creates a socket and connects it to the specified port number at
the specified IP address.
If the stream argument is true, this creates a
stream socket. If the stream argument is false, it
creates a datagram socket.
If the application has specified a server socket factory, that
factory's createSocketImpl method is called to create
the actual socket implementation. Otherwise a "plain" socket is created.
If there is a security manager, its
checkConnect method is called
with host.getHostAddress() and port
as its arguments. This could result in a SecurityException.
If UDP socket is used, TCP/IP related socket options will not apply.
- Parameters:
host - the IP address.
port - the port number.
stream - if true, create a stream socket;
otherwise, create a datagram socket.
- Throws:
IOException - if an I/O error occurs when creating the socket.
SecurityException - if a security manager exists and its
checkConnect method doesn't allow the operation.
IllegalArgumentException - if the port parameter is outside
the specified range of valid port values, which is between
0 and 65535, inclusive.
NullPointerException - if host is null.
- See Also:
setSocketImplFactory(java.net.SocketImplFactory),
SocketImpl,
SocketImplFactory.createSocketImpl(),
SecurityManager.checkConnect(java.lang.String, int)
-
Method Detail
-
connect
public void connect(SocketAddress endpoint,
int timeout)
throws IOException
Connects this socket to the server with a specified timeout value.
A timeout of zero is interpreted as an infinite timeout. The connection
will then block until established or an error occurs.
- Parameters:
endpoint - the SocketAddress
timeout - the timeout value to be used in milliseconds.
- Throws:
IOException - if an error occurs during the connection
SocketTimeoutException - if timeout expires before connecting
IllegalBlockingModeException - if this socket has an associated channel,
and the channel is in non-blocking mode
IllegalArgumentException - if endpoint is null or is a
SocketAddress subclass not supported by this socket
- Since:
- 1.4
-
getPort
public int getPort()
Returns the remote port number to which this socket is connected.
If the socket was connected prior to being closed,
then this method will continue to return the connected port number
after the socket is closed.
- Returns:
- the remote port number to which this socket is connected, or
0 if the socket is not connected yet.
-
sendUrgentData
public void sendUrgentData(int data)
throws IOException
Send one byte of urgent data on the socket. The byte to be sent is the lowest eight
bits of the data parameter. The urgent byte is
sent after any preceding writes to the socket OutputStream
and before any future writes to the OutputStream.
- Parameters:
data - The byte of data to send
- Throws:
IOException - if there is an error
sending the data.
- Since:
- 1.4
-
shutdownOutput
public void shutdownOutput()
throws IOException
Disables the output stream for this socket.
For a TCP socket, any previously written data will be sent
followed by TCP's normal connection termination sequence.
If you write to a socket output stream after invoking
shutdownOutput() on the socket, the stream will throw
an IOException.
- Throws:
IOException - if an I/O error occurs when shutting down this
socket.
- Since:
- 1.3
- See Also:
shutdownInput(),
close(),
setSoLinger(boolean, int),
isOutputShutdown()
-
isBound
public boolean isBound()
Returns the binding state of the socket.
Note: Closing a socket doesn't clear its binding state, which means
this method will return true for a closed socket
(see isClosed()) if it was successfuly bound prior
to being closed.
- Returns:
- true if the socket was successfuly bound to an address
- Since:
- 1.4
- See Also:
bind(java.net.SocketAddress)
Java™ Platform Standard Ed. 8
Submit a bug or feature For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples. Copyright © 1993, 2026, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
|