title: java.net.BindException: Cannot assign requested address: no further information
body: <p>I'm using TCP from Akka IO to make a TCP connection. I have a TCP socket listening on 127.0.0.1 port 2001 and I'm trying to connect to that socket, but from the network interface with address 192.168.101.20, which is the (local) address of my Ethernet adapter. When I do this on a Linux OS everything works fine; I can create a connection and I'm able send and receive messages. When I, however, do this on a Windows (7) OS I cannot seem to get a connection. All I get is the following exception:</p>
  
  <blockquote>
    <p>java.net.BindException: Cannot assign requested address: no further information</p>
  </blockquote>
  
  <p>Since I first encountered this with the Akka IO module I extracted the Java code responsible for making the connection and adjusted it to be run in a simple main class:</p>
  
  <pre class="lang-java prettyprint-override"><code>final InetSocketAddress remoteAddress = new InetSocketAddress("127.0.0.1", 2001);
  final InetSocketAddress localAddress = new InetSocketAddress("192.168.101.20", 0);
  new Thread(() -&gt; {
      try {
          final Socket socket = new ServerSocket(remoteAddress.getPort(), 100,
                  remoteAddress.getAddress()).accept();
          System.out.println("Connection astablished with ["
                  + socket.getInetAddress() + "]");
      } catch (IOException ex) {
      }
  }).start();
  System.out.println("Attempting connection to [" + remoteAddress + "]");
  
  /* Code making a connection */
  final SocketChannel channel = (SocketChannel) SocketChannel.open()
          .configureBlocking(false);
  channel.socket().bind(localAddress);
  if (!channel.connect(remoteAddress)) {
      int retries = 3;
      while (!channel.finishConnect() &amp;&amp; --retries &gt; 0) {  // Throws BindException
          Thread.sleep(100);
      }
      if (retries == 0) {
          throw new IOException("Could not establish connection");
      }
  }
  </code></pre>
  
  <p><em>Note: The local address might need to be changed depending on the address of the Ethernet adapter.</em></p>
  
  <p>This can also be shown by using a simple <code>Socket</code> implementation:</p>
  
  <pre class="lang-java prettyprint-override"><code>/* Code making a connection */
  final Socket socket = new Socket();
  socket.bind(localAddress);
  socket.connect(remoteAddress); // Throws ConnectException
  </code></pre>
  
  <blockquote>
    <p>java.net.ConnectException: connect: Address is invalid on local machine, or port is not valid on remote machine</p>
  </blockquote>
  
  <p>Anybody an idea why this works on Linux, but not on Windows?</p>