Java Socket getLocalSocketAddress() method

The getLocalSocketAddress() method of Java Socket class returns the address of the endpoint to which this socket is bound. This method will continue to return an InetSocketAddress even after the socket is closed.

Syntax

Parameter

NA

Return

The getLocalSocketAddress() method returns:

  • the SocketAddress, representing the local endpoint of this socket.
  • null, if the socket is not bounded.
  • loopback SocketAddress, if it is denied by the security manager.

Example 1

Test it Now

Output:

Local Port number: 1085
Local socket address: /127.0.0.1:1085

Example 2

Test it Now

Output:

Local Port number: 1085
Local socket address: /192.168.0.102:1085

Example 3

Test it Now

Output:

Local socket address: null




Latest Courses