• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 10038
  • Last Modified:

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

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
Gezna
Asked:
Gezna
1 Solution
 
Ajay-SinghCommented:
the host should either be localhost, or any other host address of local computer - should never be remote host address
0
 
enachemcCommented:
make sure that there is no other process that uses that port.
0
 
geoff2kCommented:
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now