Home » Programming Languages » JAVA Programs » Solved : java.net.BindException: Address already in use (Bind failed)

Solved : java.net.BindException: Address already in use (Bind failed)

[addthis tool="addthis_inline_share_toolbox"]

If you are working with TCP / UDP sockets in JAVA, there is higher chances your might have seen an exception like below, while trying to create a new socket on specific predefined port number instead of using dynamically allocated port number.

Exception in thread "main" java.net.BindException: Address already in use (Bind failed)
	at java.net.PlainDatagramSocketImpl.bind0(Native Method)
	at java.net.AbstractPlainDatagramSocketImpl.bind(AbstractPlainDatagramSocketImpl.java:93)
	at java.net.DatagramSocket.bind(DatagramSocket.java:392)
	at java.net.DatagramSocket.<init>(DatagramSocket.java:242)
	at java.net.DatagramSocket.<init>(DatagramSocket.java:299)
	at java.net.DatagramSocket.<init>(DatagramSocket.java:271)
	at DatagramReceiverServer.main(DatagramReceiverServer.java:9)

Here, we had used the sample DatagramReceiverServer.java from “JAVA DatagramSocket Client and Server Example” as an example. If we compile, this below java code and run it once from one terminal and then run the same program from another terminal, we will get the similar exception as above on the second terminal.

Program which can throw above exception, if run twice

import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class DatagramReceiverServer {
        private static int remoteServerPort = 1900;

        public static void main(String[] args) throws Exception {
                DatagramSocket localServerSocket = new DatagramSocket(remoteServerPort);
                byte[] buf = new byte[1024];

                DatagramPacket datagramReceiverPacket = new DatagramPacket(buf, 1024);
                localServerSocket.receive(datagramReceiverPacket);

                String receivedMessage = new String(datagramReceiverPacket.getData(), 0, datagramReceiverPacket.getLength());
                System.out.println(receivedMessage);
                localServerSocket.close();
        }

}

Solution : Modified program to let the system allocate the dynamic port everytime we run this program.

import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class DatagramReceiverServer {
        private static int remoteServerPort = 0;

        public static void main(String[] args) throws Exception {
                DatagramSocket localServerSocket = new DatagramSocket();
                remoteServerPort = localServerSocket.getLocalPort();
                System.out.println("server running on port : " + remoteServerPort);
                byte[] buf = new byte[1024];

                DatagramPacket datagramReceiverPacket = new DatagramPacket(buf, 1024);
                localServerSocket.receive(datagramReceiverPacket);

                String receivedMessage = new String(datagramReceiverPacket.getData(), 0, datagramReceiverPacket.getLength());
                System.out.println(receivedMessage);
                localServerSocket.close();
        }

}

In above program, only lines code we changed is to use dynamic port as,

private static int remoteServerPort = 0;
DatagramSocket localServerSocket = new DatagramSocket();
remoteServerPort = localServerSocket.getLocalPort();
System.out.println("server running on port : " + remoteServerPort);

Note: if you do not know, which is the another application using the same port as of yours, please refer to “Identify which application / process is using which port or keeping it busy on android / Linux”

Leave a Comment