Java Socket getInputStream() method25 Mar 2025 | 2 min read 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. SyntaxParameterNA ReturnThe getInputStream() returns an input stream for reading bytes from this socket. ThrowsIOException- 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 1Output: Input Stream: java.io.DataInputStream@1540e19d Example 2Output: Exception in thread "main" java.net.SocketException: Socket is closed at java.net.Socket.getInputStream(Socket.java:903) at com.tpointtech.JavaSocketGetInputStreamExample2.main(JavaSocketGetInputStreamExample2.java:24) Example 3Output: Exception in thread "main" java.net.SocketException: Socket is not connected at java.net.Socket.getInputStream(Socket.java:905) at com.tpointtech.JavaSocketGetInputStreamExample3.main(JavaSocketGetInputStreamExample3.java:20) Next TopicJava Socket Class |
Java method The isInputShutdown () method of Java Socket class returns a Boolean value 'true' if the read-half of the socket connection has been closed, else it returns false. Syntax public boolean isInputShutdown () Parameter NA Return The isInputShutdown () method returns a Boolean value 'true' if the socket has been...
3 min read
Java method The getSendBufferSize () method of Java Socket class returns the buffer size(SO_SNDBUF) used by the platform for output on this socket. Syntax public int getSendBufferSize () Parameter NA Return The getSendBufferSize () method returns the value of the SO_SNDBUF option of the specified Socket. Throws SocketException - if an error...
2 min read
Java 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 public InetAddress getInetAddress () Parameter NA Return The getInetAddress () method either returns the...
3 min read
Java Socket setReuseAddress () method The setReuseAddress () method of Java Socket class enables or disables the SO_REUSEADDR socket option. The initial setting of SO_REUSEADDR is disabled. Syntax public void setReuseAddress (boolean on) throws SocketException Parameter The parameter 'on' states whether the SO_REUSEADDR option should be enabled or disabled. Return NA Throws SocketException -...
3 min read
Java method The setSoTimeout () method of Java Socket class enables or disables the SO_TIMEOUT option with the given timeout value, in milliseconds. The timeout value should be greater than 0 otherwise, it will throw an error. Syntax public void setSoTimeout (int timeout) throws SocketException Parameter The...
3 min read
Java Socket setSendBufferSize () method The setSendBufferSize () method of Java Socket class sets the SO_SNDBUF option to the given value for this socket. The size value should be greater than 0. Syntax public void setSendBufferSize (int size) throws SocketException Parameter The parameter 'size' represents the size to which the...
2 min read
Java method The getLocalPort() method of Java Socket class returns the local port number to which the specified socket is bound. If the socket was being closed, this method will return the connected port number after the socket is closed. Syntax public int getLocalPort() Parameter NA Return The getLocalPort () method...
2 min read
Java method The getPort() method of Java Socket class returns the remote port number to which the socket is connected. This method will return the port number even after the socket is closed. Syntax public int getPort () Parameter NA Return The getPort() method either returns the port number to which...
2 min read
Java method The getKeepAlive() method of Java Socket class tests whether SO_KEEPALIVE option is enabled or not. Syntax public boolean getKeepAlive () Parameter NA Return The getKeepAlive() method either returns a Boolean indicating whether SO_KEEPALIVE option is enabled or not. Throws SocketException - if there is an error in the underlying protocol, such...
2 min read
Java method The getChannel() method of Java Socket class returns the unique SocketChannel object linked with this socket. Syntax public SocketChannel getChannel() Parameter NA Return The getChannel() method either returns the socket channel linked with this socket or returns null if the socket was not created for the specified channel. Example...
2 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India