Javatpoint Logo
Javatpoint Logo

Java ServerSocket bind() method

The bind() method of Java ServerSocket class binds the ServerSocket to the specified socket address, i.e. IP address and port number.

If the specified address is null, the system will automatically pick up an ephemeral port and a valid local address to bind this socket.

In the second syntax, the backlog argument is the requested maximum number of pending connections on the socket. The specified backlog should always be greater than 0. If the backlog's value is less than or equal to 0, then an implementation specific default value will be used.

Syntax

Parameter

  1. The parameter 'endpoint' represents the IP address and port number to bind to.
  2. The parameter 'backlog' represents the maximum length of the queue of incoming connections.

Return

NA

Throws

IOException- if the socket is already bound or bound if the bind operation fails

SecurityException- if the SecurityManager is present and its checkListenmethod doesn't permit the operation

IllegalArgumentException- if the endpoint is a SocketAddress subclass which is not supported by this socket

Example 1

Output:

Local Socket Address: localhost/127.0.0.1:1085

Example 2

Output:

Exception in thread "main" java.net.SocketException: Socket is closed
	atjava.net.ServerSocket.bind(ServerSocket.java:359)
	atjava.net.ServerSocket.bind(ServerSocket.java:329)
	at com.javaTpoint.JavaServerSocketBindExample2.main(JavaServerSocketBindExample2.java:21)

Example 3

Output:

Local Socket Address: localhost/127.0.0.1:1085

Example 4

Output:

Local Socket Address: localhost/127.0.0.1:1085
Channel: null
Next TopicJava ServerSocket





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA