Java Socket getInetAddress() method

The getInetAddress() method of Java Socket class returns the address to which the socket is connected. If the socket is being closed, this method will return the connected address after the socket is closed.

Syntax

Parameter

NA

Return

The getInetAddress () method either returns the remote IP address to which the socket is connected or returns null if the socket is not connected.

Example 1

Test it Now

Output:

Inet address: localhost/127.0.0.1
Port number: 1085

Example 2

Test it Now

Output:

Exception in thread "main" java.net.SocketException: Socket is closed
	at java.net.Socket.connect(Socket.java:566)
	at com.javaTpoint.JavaSocketGetInetAddressExample2.main(JavaSocketGetInetAddressExample2.java:20)




Latest Courses