title: Unable to connect server java.net.BindException: Cannot assign requested address: bind
body: <p>Working fine when i connect the localhost(127.0.0.1), but unable to connect when i connect to 12.11.123.121. Getting below error.</p>
  
  <p>Note: Given Socket server is dummy one.
  <strong>Error</strong></p>
  
  <pre><code>java.net.BindException: Cannot assign requested address: bind
      at sun.nio.ch.Net.bind0(Native Method)
      at sun.nio.ch.Net.bind(Unknown Source)
      at sun.nio.ch.Net.bind(Unknown Source)
      at sun.nio.ch.AsynchronousServerSocketChannelImpl.bind(Unknown Source)
      at java.nio.channels.AsynchronousServerSocketChannel.bind(Unknown Source)
      at com.epro.welcome.ComputerInstruClient.serverStart(ComputerInstruClient.java:40)
      at com.epro.welcome.ComputerInstruClient.access$0(ComputerInstruClient.java:36)
      at com.epro.welcome.ComputerInstruClient$1.run(ComputerInstruClient.java:70)
      at java.lang.Thread.run(Unknown Source)
  </code></pre>
  
  <p><strong>Code</strong></p>
  
  <pre><code>import java.io.InputStream;
  import java.io.ObjectInputStream;
  import java.io.ObjectOutputStream;
  import java.io.OutputStream;
  import java.net.InetAddress;
  import java.net.InetSocketAddress;
  import java.nio.channels.AsynchronousServerSocketChannel;
  import java.nio.channels.AsynchronousSocketChannel;
  import java.nio.channels.Channels;
  import java.util.concurrent.Future;
  
  public class Test {
    private static void clientStart() {
      try {
        InetSocketAddress  hostAddress = new InetSocketAddress(InetAddress.getByName("12.11.123.121"),
            2583);
        AsynchronousSocketChannel clientSocketChannel = AsynchronousSocketChannel
            .open();
        Future&lt;Void&gt; connectFuture = clientSocketChannel.connect(hostAddress);
        connectFuture.get(); // Wait until connection is done.
        OutputStream os = Channels.newOutputStream(clientSocketChannel);
        ObjectOutputStream oos = new ObjectOutputStream(os);
        for (int i = 0; i &lt; 5; i++) {
          oos.writeObject("Look at me " + i);
          Thread.sleep(1000);
        }
        oos.writeObject("EOF");
        oos.close();
        clientSocketChannel.close();
      } catch (Exception e) {
        e.printStackTrace();
      }
  
    }
  
    private static void serverStart() {
      try {
        InetSocketAddress  hostAddress = new InetSocketAddress(InetAddress.getByName("12.11.123.121"),
            2583);
        AsynchronousServerSocketChannel serverSocketChannel = AsynchronousServerSocketChannel
            .open().bind(hostAddress);
        Future&lt;AsynchronousSocketChannel&gt; serverFuture = serverSocketChannel
            .accept();
        final AsynchronousSocketChannel clientSocket = serverFuture.get();
        if ((clientSocket != null) &amp;&amp; (clientSocket.isOpen())) {
          InputStream connectionInputStream = Channels
              .newInputStream(clientSocket);
          ObjectInputStream ois = null;
          ois = new ObjectInputStream(connectionInputStream);
          while (true) {
            Object object = ois.readObject();
            if (object.equals("EOF")) {
              clientSocket.close();
              break;
            }
            System.out.println("Received :" + object);
          }
          ois.close();
          connectionInputStream.close();
        }
  
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  
    public static void main(String[] args) throws Exception{
      Thread serverThread = new Thread(new Runnable() {
        @Override
        public void run() {
          serverStart();
        }
      });
      serverThread.start();
      Thread clientThread = new Thread(new Runnable() {
        @Override
        public void run() {
          clientStart();
        }
      });
      clientThread.start();
   }
  
  }
  </code></pre>