?
Solved

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

Posted on 2006-11-30
3
Medium Priority
?
9,903 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 1500 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month12 days, 9 hours left to enroll

777 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