Java Socket getRemoteSocketAddress() method

The getRemoteSocketAddress () method of Java Socket class returns the address of the endpoint of the specified socket if it is connected else it will return null if this socket is not connected.

Syntax

Parameter

NA

Return

The getRemoteSocketAddress () method either returns the SocketAddress indicating the remote endpoint of the socket if the socket is connected else it returns null if the socket is not connected.

Example 1

Test it Now

Output:

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

Example 2

Test it Now

Output:

Remote socket address: null

Example 3

Test it Now

Output:

Exception in thread "main" java.net.ConnectException: Connection refused: connect
	at java.net.DualStackPlainSocketImpl.connect0(Native Method)
	at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
	at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
	at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
	at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
	at java.net.Socket.connect(Socket.java:589)
	at java.net.Socket.connect(Socket.java:538)
	at com.javaTpoint. JavaSocketGetRemoteSocketAddressExample3.main(
	JavaSocketGetRemoteSocketAddressExample3.java:15)




Latest Courses