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

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.(DatagramSocket.java:242)
	at java.net.DatagramSocket.(DatagramSocket.java:299)
	at java.net.DatagramSocket.(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”

Android Android Commands Android Java Applications Application Libraries Bash / Shell Scripts Bluetooth driver Build Frameworks Commands and Packages Content Management System - CMS Core Kernel C Programs Development Environment Setup Errors & Failures File Systems git Go Language Programs Hardware Platforms Home HTML JAVA Programs Kernel & Device Drivers Kernel Booting and Porting Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL NDK / Middleware / HAL Network Driver OS Concepts PHP Programming Languages RaspberryPi Scripting and Automation Search Engine Optimisation ( SEO ) Social Media Socurce Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized Userspace Utilities Web design and development Wordpress Yocto / Bitbake / Openembedded

Leave a Reply