Solved

java.net.BindException: Cannot assign requested address: Cannot bind

Posted on 2006-11-30
3
9,638 Views
Last Modified: 2012-06-27
Hi,

When I try to connect to a machine (229.2.0.2 on port 9202) I get the java.net.BindException.

When I try connecting to localhost, I do not get the exception.  Also, I do not get an exception when I try connecting to a machine on my local network.

I would like to know why I cannot bind to this address, and I would like to know a workaround or solution.

Thanks!

Output of TestServer:

Host Address=229.2.0.1
Port        =9201
Exception in thread "main" java.net.BindException: Cannot assign requested address: Cannot bind
      at java.net.PlainDatagramSocketImpl.bind0(Native Method)
      at java.net.PlainDatagramSocketImpl.bind(PlainDatagramSocketImpl.java:82)
      at java.net.DatagramSocket.bind(DatagramSocket.java:368)
      at java.net.MulticastSocket.<init>(MulticastSocket.java:147)
      at TestServer.main(TestServer.java:17)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:585)
      at com.intellij.rt.execution.application.AppMain.main(AppMain.java:90)

TestServer source code:

import java.net.*;

public class TestServer {

 public static void main(String[] args) throws Exception {

   if (args.length != 2) {
     System.out.println("Usage: java TestServer <host_address_ipv6> <port>");
     System.exit(1);
   }

   System.out.println("Host Address="+args[0]);
   System.out.println("Port        ="+args[1]);

   ServerSocket ss = new ServerSocket();
   InetSocketAddress isa = new InetSocketAddress(InetAddress.getByName(args[0]), Integer.parseInt(args[1]));
   new MulticastSocket(isa);

   System.out.println("ServerSocket Bound");

 //  ss.accept();
 }
}
0
Comment
Question by:Gezna
3 Comments
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 18047358
the host should either be localhost, or any other host address of local computer - should never be remote host address
0
 
LVL 12

Expert Comment

by:enachemc
ID: 18048221
make sure that there is no other process that uses that port.
0
 

Accepted Solution

by:
geoff2k earned 500 total points
ID: 18048679
You can only bind to ip addresses that are bound to an adapter on your local machine.   Under Windows you can use "ipconfig /all" to get a list of all the addresses that are local to you:

c:>ipconfig /all | find "IP Address"
        IP Address. . . . . . . . . . . . : 192.168.47.1
        IP Address. . . . . . . . . . . . : 192.168.159.1
        IP Address. . . . . . . . . . . . : 10.0.0.14
        IP Address. . . . . . . . . . . . : 10.31.0.240

I can successfully bind to any of the above addresses as well as 127.0.0.1 (localhost).  Any other procudes the error you mention.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now