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 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 setTrafficClass() method of Java Socket class sets the traffic class or type-of-service in the IP header for packets sent from the specified socket. The parameter (tc) should be in the range from 0 to 255(0<=tc<=255) else an IllegalArgumentException will be thrown. Syntax public void...
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 setKeepAlive() 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 void setKeepAlive (boolean on) throws SocketException Parameter The parameter 'on' represents whether the socket should be enabled or...
2 min read
Java method The isConnected () method of Java Socket class returns the connection state of the socket. This method would continue to return the connection state which was there before being closed. Syntax public boolean isConnected() Parameter NA Return The isConnected () method would return a Boolean value 'true' if the...
3 min read
Java method The getTrafficClass() method of Java Socket class returns the traffic class or type-of-service in the IP header sent from the specified Socket. Syntax public int getTrafficClass () Throws SocketException Parameter NA Return The getTrafficClass () method returns the traffic class or type-of-service. Throws SocketException - if there occurs an error while...
2 min read
Java method The getSoLinger () method of Java Socket class returns the setting if the SO_Linger option is set else it returns -1 if the option is disabled. This method only affects the setting for SO_LINGER. Syntax public int getSoLinger () Parameter NA Return The getSoLinger () method returns the setting for...
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 sendUrgentData() method of Java Socket class sends one byte of urgent data on the socket. The byte should be the lowest eight bits of the data parameter. Syntax public void sendUrgentData (int data) throws IOException Parameter The "data" parameter represents the byte of data to send. Return NA Throws IOException...
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
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