Java Socket toString() method

The toString() method of Java Socket class converts this socket to a String.

Syntax

Parameter

NA

Overrides

The toString() method in class Object

Return

The toString() method returns the string representation for the given socket.

Example 1

Test it Now

Output:

Inet address: localhost/127.0.0.1
Port number: 1085
Socket[addr=localhost/127.0.0.1,port=1085,localport=1085]

Example 2

Test it Now

Output:

Inet address: localhost/127.0.0.1
Port number: 1085
Socket[addr=localhost/127.0.0.1,port=1085,localport=1085]

Example 3

Test it Now

Output:

Exception in thread "main" java.net.SocketException: Socket is not connected
	at java.net.Socket.shutdownOutput(Socket.java:1553)
	at com.javaTpoint.JavaSocketToStringExample3.main(JavaSocketToStringExample3.java:11)




Latest Courses