Java Socket Class

The Socket class acts as an endpoint for communication between two machines. It implements client sockets.

Methods

MethodDescription
bind(SocketAddress bindpoint)This method binds the given socket to the specified local address.
close()This method closes the socket.
connect(SocketAddress endpoint)
connect(SocketAddress endpoint, int timeout)
The connect() method connects this socket to the server.
This method connects this socket to the server with a specified timeout value.
getChannel()It returns a unique SocketChannel object associated with this socket.
getInetAddress()It returns the address to which the socket is connected.
getInputStream()It returns an input stream for the specified socket.
getKeepAlive()This method tests if the SO_KEEPALIVE option is enabled or not.
getLocalAddress()It returns the local address to which the given socket is bound.
getLocalPort()It gets the local port number to which the specified socket is bound.
getLocalSocketAddress()This method gets the address of the endpoint to which this socket is bound.
getOOBInline()This method tests whether the SO_OOBINLINE option is enabled or not.
getOutputStream()This method returns an output stream for the specified socket.
getPort()This method returns the remote port number to which the socket is connected.
getReceiveBufferSize()It returns the value of the SO_RCVBUF option for this Socket.
getRemoteSocketAddress()This method either returns the address of the endpoint to which this socket is connected or it returns null if the socket is unconnected.
getReuseAddress()It tests whether the SO_REUSEADDR option is enabled or not.
getSendBufferSize()This method gets the value of the SO_SNDBUF option for this Socket.
getSoLinger()This method returns setting for the SO_LINGER option.
getSoTimeout()It returns the setting for the SO_TIMEOUT option if it is enabled, else it returns 0 if the option is disabled.
getTcpNoDelay()This method tests whether the TCP_NODELAY option is enabled or not.
getTrafficClass()This method returns the traffic class or type-of-service in the IP header for packets sent from this Socket
isBound()It returns the binding state for this socket.
isClosed()This method returns the closed state of this socket.
isConnected()This method returns the connection state of the socket.
isInputShutdown()It returns whether the read-half of the socket connection is closed or not.
isOutputShutdown()It returns whether the write-half of the socket connection is closed or not.
sendUrgentData(int data)This method sends one byte of urgent data on the socket.
setKeepAlive(boolean on)This method enables or disables the SO_KEEPALIVE option for this socket.
setOOBInline(boolean on)This method enables or disables the SO_OOBINLINE option for this socket.
setReceiveBufferSize(int size)This method returns the SO_RCVBUF option to the specified value for this Socket.
setReuseAddress(boolean on)This method enables or disables the SO_REUSEADDR socket option for this socket.
setSendBufferSize(int size)This method returns the SO_SNDBUF option to the specified value for this Socket.
setSoLinger(boolean on, int linger)It enables or disables the SO_LINGER option with the specified linger time in seconds.
setSoTimeout(int timeout)This method enables or disables the SO_TIMEOUT option with the specified time in milliseconds.
setTcpNoDelay(boolean on)This method enables or disables the TCP_NODELAY option for this socket.
setTrafficClass(int tc)It returns the traffic class or type-of-service octet in the IP header for packets sent from the Socket.
shutdownInput()This method places the input stream for this socket at the end of this stream.
shutdownOutput()This method disables the output stream for this socket.
toString()This method converts this socket to a String.

Example 1

Test it Now

Output:

Send Buffer size: 67
SO_KEEPALIVE option is disabled
Remote socket address: localhost/127.0.0.1:1085

Example 2

Test it Now

Output:

Send Buffer size: 90
Timeout value: 90
SO_TIMEOUT option is enabled: true
SO_KEEPALIVE is enabled: true
Local socket address: /127.0.0.1:1085

Example 3

Test it Now

Output:

Inet address: localhost/127.0.0.1
Buffer size: 10
SO_REUSEADDR option is enabled: true
The socket is closed: true
Socket[addr=localhost/127.0.0.1,port=1085,localport=1085]

Example 4

Test it Now

Output:

The socket is connected: true
The socket is binded: true
Local Port: 1085
Output Stream: java.io.DataOutputStream@1540e19d




Latest Courses