Socket

public class Socket extends Object
implements Closeable
Known Direct Subclasses

Provides a client-side TCP socket.

Public Constructor Summary

Socket()
Creates a new unconnected socket.
Socket(Proxy proxy)
Creates a new unconnected socket using the given proxy type.
Socket(String dstName, int dstPort)
Creates a new streaming socket connected to the target host specified by the parameters dstName and dstPort.
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.
Socket(String hostName, int port, boolean streaming)
This constructor was deprecated. Use Socket(String, int) instead of this for streaming sockets or an appropriate constructor of DatagramSocket for UDP transport.
Socket(InetAddress dstAddress, int dstPort)
Creates a new streaming socket connected to the target host specified by the parameters dstAddress and dstPort.
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(InetAddress addr, int port, boolean streaming)
This constructor was deprecated. Use Socket(InetAddress, int) instead of this for streaming sockets or an appropriate constructor of DatagramSocket for UDP transport.

Protected Constructor Summary

Socket(SocketImpl impl)
Creates an unconnected socket with the given socket implementation.

Public Method Summary

void
bind(SocketAddress localAddr)
Binds this socket to the given local host address and port specified by the SocketAddress localAddr.
synchronized void
close()
Closes the socket.
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.
void
connect(SocketAddress remoteAddr)
Connects this socket to the given remote host address and port specified by the SocketAddress remoteAddr.
SocketChannel
getChannel()
Returns this socket's SocketChannel, if one exists.
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 SO_KEEPALIVE setting.
InetAddress
getLocalAddress()
Returns the local IP address this socket is bound to, or an address for which 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 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.
synchronized 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 SO_REUSEADDR setting.
synchronized 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.
synchronized int
getSoTimeout()
Returns this socket's receive timeout.
boolean
getTcpNoDelay()
Returns this socket's SocketOptions#TCP_NODELAY setting.
int
getTrafficClass()
Returns this socket's 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
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 SO_KEEPALIVE option.
void