Java Socket bind() method25 Mar 2025 | 2 min read The bind() method of Java Socket class binds the socket to a local address. If the specified address is null, then the system will automatically pick up a port number and a valid local address to bind with the socket. SyntaxParameterThe parameter 'bindpoint' represents the SocketAddress to bind. ReturnNA ThrowsIOException - if the bind operation fails, or if the socket is already bound. IllegalArgumentException - if the bindpoint is a SocketAddress subclass not supported by this socket. SecurityException - if the security manager exists and its checkListen method doesn't allow the bind to the local port. Example 1Output: Inet address: null Port number: 1085 Example 2Output: Exception in thread "main" java.lang.IllegalArgumentException: port out of range:-1085 at java.net.InetSocketAddress.checkPort(InetSocketAddress.java:143) at java.net.InetSocketAddress. Example 3Output: Exception in thread "main" java.net.SocketException: Socket is closed at java.net.Socket.bind(Socket.java:625) at com.tpointtech.JavaSocketBindExample3.main(JavaSocketBindExample3.java:17) Next TopicJava Socket Class |
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 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 method The shutdownOutput() method of Java Socket class disables the output stream for the specified socket. If the socket output stream is written after invoking the shutdownOutput() method on the socket, the stream will throw an IOException. Syntax public void shutdownOutput () throws IOException Parameter NA Return NA Throws IOException...
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 Socket getReuseAddress () method The getReuseAddress () method of Java Socket class tests whether the SO_REUSEADDR option is enabled or not. It throws SocketException if finds an error in the underlying protocol, such as a TCP error. Syntax public boolean getReuseAddress() Parameter NA Return The getReuseAddress () method returns a...
2 min read
Java The Socket class acts as an endpoint for communication between two machines. It implements client sockets. Methods Method Description bind(SocketAddress bindpoint) This method binds the given socket to the specified local address. close() This method closes the socket. connect(SocketAddress endpoint) connect(SocketAddress endpoint, int timeout) The connect() method connects this socket to the server. This method...
8 min read
Java method The setOOBInline() method of Java Socket class enables or disables the SO_OOBInline. By default, the SO_OOBInline option is disabled. Syntax public void setOOBInline (Boolean on) throws SocketException Parameter The 'on' represents whether the SO_OOBInline option should enabled or not. Return NA Throws SocketException - if there is an error in the...
2 min read
Java method 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. Syntax public OutputStream getOutputStream() throws IOException Parameter NA Return The getOutputStream() returns an output stream for writing bytes to this socket. Throws IOException-...
3 min read
Java method connect(SocketAddress endpoint) The connect() method of Java Socket class connects the specified socket to the server. Syntax public void connect(SocketAddress endpoint) throws IOException Parameter The parameter 'endpoint' represents the SocketAddress. Return NA Throws IOException - if an error occurs during the connection. IllegalBlockingModeException - if this socket has an associated channel and the...
5 min read
Java method The getReceiveBufferSize() method of Java Socket class returns the buffer size (SO_RCVBUF option) for this socket. Syntax public int getReceiveBufferSize () throws SocketException Parameter NA Returns The getReceiveBufferSize() method returns the buffer size or the SO_RCVBUF option for this socket. Throws SocketException - if there is an error in the underlying...
3 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