exceptions

java.net.BindException – How to handle BindException

In this example we will discuss about BindException in Java. This exception is thrown to indicate that an error occurred when an application attempts to bind a socket to a local address and port. The main cause of this exception is that either the port is already in use, or the requested address cannot be assigned to the calling application.

The BindException class extends the SocketException class, which is thrown to indicate an error while creating or accessing a Socket. In addition, the SocketException class extends the IOException class, which is used to indicate that an I/O exception has been occurred.

Finally, the BindException exists since the 1.1 version of Java.

The Structure of BindException

Constructors

  • BindException()
  • Creates an instance of the BindException class, setting null as its message.

  • BindException(String s)
  • Creates an instance of the BindException class, using the specified string as message. The string argument indicates the name of the class that threw the error.

The BindException in Java

As we already explained, the BindException is thrown when your Java application tries to bind a socket to a local address and port, but they are being used by another application. Take a closer look to the following example:

BindExceptionExample.java:

import java.io.IOException;
import java.net.ServerSocket;

public class BindExceptionExample {
	
	private final static int PORT = 15000;
	
	public static void main(String[] args) throws IOException {
		ServerSocket _socket = new ServerSocket(PORT);
		
		//The following statement throws a BindException.
		ServerSocket _socket_ = new ServerSocket(PORT);
		
		_socket.close();
		_socket_.close();
	}
}

In this example we want to create two instances of the ServerSocket class. The first instance is successfully created and binds the specified port. When the second instance is about to be created, using the same port, then, a BindException is thrown.

A sample execution is shown below:

Exception in thread "main" java.net.BindException: Address already in use
	at java.net.PlainSocketImpl.socketBind(Native Method)
	at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:382)
	at java.net.ServerSocket.bind(ServerSocket.java:375)
	at java.net.ServerSocket.(ServerSocket.java:237)
	at java.net.ServerSocket.(ServerSocket.java:128)
	at main.java.BindExceptionExample.main(BindExceptionExample.java:14)

The simplest way to avoid this exception is to use another port and verify that the port is not in use by another application.

 
This was a tutorial about BindException in Java.

Sotirios-Efstathios Maneas

Sotirios-Efstathios (Stathis) Maneas is a PhD student at the Department of Computer Science at the University of Toronto. His main interests include distributed systems, storage systems, file systems, and operating systems.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Gideon Pereira
Gideon Pereira
3 years ago

Thanks for this explanation.

Back to top button