Java Socket getOutputStream() method25 Mar 2025 | 2 min read The getOutputStream() method of Java Socket class returns an output stream for the given socket. If you close the returned OutputStream then it will close the linked socket. SyntaxParameterNA ReturnThe getOutputStream() returns an output stream for writing bytes to this socket. ThrowsIOException- if an I/O error occurs when creating the output stream or if the socket is not connected. Example 1Output: Output Stream: java.io.DataOutputStream@1540e19d Example 2Output: Exception in thread "main" java.net.SocketException: Socket is closed at java.net.Socket.getOutputStream(Socket.java:943) at com.tpointtech.JavaSocketGetOutputStreamExample2.main(JavaSocketGetOutputStreamExample2.java:22) Example 3Output: Exception in thread "main" java.net.SocketException: Socket is not connected at java.net.Socket.getOutputStream(Socket.java:945) at com.tpointtech.JavaSocketGetOutputStreamExample3.main(JavaSocketGetOutputStreamExample3.java:20) Next TopicJava Socket Class |
Java method The setSoLinger() method of Java Socket class enables or disables the SO_LINGER option with the given linger time in seconds. It is used to specify how the close() method affects socket using a connection-oriented protocol. The timeout value is platform-specific, and this setting...
4 min read
Java method The close() method of Java Socket class closes the specified socket. Once the socket has been closed, it is not available for further networking use. If the socket has a channel, the channel is also closed. Syntax public void close () throws IOException Parameter NA Return NA Specified by The close()...
2 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 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 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 setTcpNoDelay () method of Java Socket class enables or disables the TCP_NODELAY option. Syntax public void setTcpNoDelay (boolean on) throws SocketException Parameter The parameter 'on' represents enables or disables the TCP_NODELAY option. Return NA Throws SocketException - if an error is there in the underlying protocol, such as...
3 min read
Java method The isBound () method of Java Socket class returns a Boolean value representing the binding state of the socket. This method would continue to return 'true' for a closed socket, if it was bound before being closed. Syntax public boolean isBound() Parameter NA Return The isBound () method would...
3 min read
Java method The isOutputShutdown () method of Java Socket class returns a Boolean value 'true' if the write-half of the socket connection has been closed successfully, else it returns false. Syntax public boolean isOutputShutdown () Parameter NA Return The isOutputShutdown () method would return a Boolean value 'true' if the output...
3 min read
Java method The toString() method of Java Socket class converts this socket to a String. Syntax public void toString() Parameter NA Overrides The toString() method in class Object Return The toString() method returns the string representation for the given socket. Example 1 import java.io.IOException; import java.net.*; public class JavaSocketToStringExample1 { ...
3 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
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