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.<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”


lynxbee_ezoic

Subscribe with Valid Email Id to receive updates in Inbox. ( Secured by Google FeedBurner )

   


Leave a Comment

Android Android Applications Android Build system Android Commands Application Libraries Application Stack / User Interface Bash / Shell Scripts Bluetooth driver Cloud Technologies Commands and Packages Compilation Content Management System Core Kernel C Programs Development & Build Development, Debugging and Performance Tools Development Environment Setup Django & REST Api Errors & Failures Git Hardware Platforms HTML JAVA Programs Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL Multimedia Audio, Video, Images NDK / Middleware / HAL OS Concepts PHP Programming Languages Scripting and Automation Search Engine Optimisation ( SEO ) Social Media Source Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized Web design and development Website Hosting Wordpress Yocto / Bitbake / Openembedded