Java Socket getInputStream() method

The getInputStream() method of Java Socket class returns an input stream for the given socket. If you close the returned InputStream, then it will close the linked socket.

Syntax

Parameter

NA

Return

The getInputStream() returns an input stream for reading bytes from this socket.

Throws

IOException- if an Input/Output error occurs while creating the input stream or if this socket is closed or the given socket is not connected, or the socket input has been shut down using shutdownInput()

Example 1

Test it Now

Output:

Input Stream: java.io.DataInputStream@1540e19d

Example 2

Test it Now

Output:

Exception in thread "main" java.net.SocketException: Socket is closed
	at java.net.Socket.getInputStream(Socket.java:903)
	at com.javaTpoint.JavaSocketGetInputStreamExample2.main(JavaSocketGetInputStreamExample2.java:24)

Example 3

Test it Now

Output:

Exception in thread "main" java.net.SocketException: Socket is not connected
	at java.net.Socket.getInputStream(Socket.java:905)
	at com.javaTpoint.JavaSocketGetInputStreamExample3.main(JavaSocketGetInputStreamExample3.java:20)




Latest Courses