title: java.net.ConnectException: Connection timed out: no further information
body: <p>Now today I was testing Server and Client code on different machine.
  Both were on same Wi-fi network.</p>
  
  <p>I created clients using below code and got this exception for many threads :</p>
  
  <p><code>java.net.ConnectException: Connection timed out: no further information
      at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
      at sun.nio.ch.SocketChannelImpl.finishConnect(Unknown Source)
      at SocketTest.connect(Client.java:188)
      at SocketTest.run(Client.java:73)
      at java.lang.Thread.run(Unknown Source)</code></p>
  
  <p>the line 73 is <code>connect(key)</code> 
  and line 188 is <code>if(!(channel.finishConnect()))</code></p>
  
  <p>So the client thread was unable to connect because no reply came from server ? Right ?</p>
  
  <p>Question)When I run both Server and Client on same machine localhost this exception does not arise. What may be the reasons ? (network problem ?).</p>
  
  <p>Also I also use Backlog queue parameter in public void bind(SocketAddress endpoint,int backlog) as 2000. While exact size is unknown(around 200 ?) but I am using a large value so that maximum value will be used.(Right ? or Java will make a queue ?).</p>
  
  <p>Can this be a reason : The Server puts the request in backlog queue and till it gets time to serve it, the timeout may have happened at Client ?</p>
  
  <p>Client :</p>
  
  <pre><code>public class Client {
  
  public static void main(String[] args) {
  
      int n=100;
      SocketTest [] st= new SocketTest[n];
      for(int i=0;i&lt;n;i++)
          st[i]= new SocketTest("hi");
  
  
      for(int i=0;i&lt;n;i++)
      {
          if(i%50 == 0)
          try{
              Thread.sleep(5000);
          }
          catch(InterruptedException ie)
          {
              System.out.println(""+ie);
          }
          new Thread(st[i]).start();
  
      }
  }
  }
  class SocketTest implements Runnable {
  
      private String message = "";
      ByteBuffer readBuffer = ByteBuffer.allocate(1000);
      private Selector selector;
      private int i;
      public static AtomicInteger cnt= new AtomicInteger(0);
  
      public SocketTest(String message){
          this.message = message;
      }
  
      @Override
      public void run() {
          SocketChannel channel;
          try {
              selector = Selector.open();
              channel = SocketChannel.open();
              channel.configureBlocking(false);
  
              channel.register(selector, SelectionKey.OP_CONNECT);
  
              channel.connect(new InetSocketAddress("192.168.1.10", 8511));
  
              while (!Thread.currentThread().isInterrupted()){
  
                  selector.select();
  
                  Iterator&lt;SelectionKey&gt; keys = selector.selectedKeys().iterator();
  
                  while (keys.hasNext()){
                      SelectionKey key = keys.next();
                      keys.remove();
  
                      if (!key.isValid()) continue;
  
                      if (key.isConnectable()){  
                              connect(key);
                          System.out.println("I am connected to the server");
                      }   
                      if (key.isWritable()){
                          write(key);
                      }
                      if (key.isReadable()){
                          read(key);
                      }
                  }   
              }
          }
          catch(ClosedByInterruptException e)
          {
              // let go of thread
          }
          catch(CancelledKeyException e){
  
              e.printStackTrace();
          }
          catch (IOException e1) {
              System.out.println("IOE Occured|maybe Server died");
              e1.printStackTrace();
          } finally {
              close();
          }
      }
  
      private void close(){
  
          try {
                  if(selector!=null)
                      selector.close();
              }
              catch(Exception e) 
              {
                      e.printStackTrace();
              }
  
      }
  
      private void read (SelectionKey key) throws IOException {
          SocketChannel channel = (SocketChannel) key.channel();
  
          readBuffer.clear();
          int length;
          try{
          length = channel.read(readBuffer);
  
          } catch (IOException e){
              System.out.println("Reading problem, closing connection for  : "+channel.getLocalAddress());
              key.cancel();
              channel.close();
              return;
          }
          if (length == -1){
              System.out.println("Nothing was read from server");
              channel.close();
              key.cancel();
              return;
          }
          readBuffer.flip();
          byte[] buff = new byte[1024];
          readBuffer.get(buff, 0, length);
          //length=buff.length;
  
          String fromserver = new String(buff,0,length,"UTF-8");
          length = fromserver.length();
          System.out.println("Server said: "+fromserver);
  
          key.interestOps(SelectionKey.OP_WRITE);
      }
  
      private void write(SelectionKey key) throws IOException {
          SocketChannel channel = (SocketChannel) key.channel();
          i++;
          message = "location now "+i;
          if(i==2)
          {
              cnt.addAndGet(1);
              System.out.println("****"+cnt.get()+"****");
          }
  
          try{
              Thread.sleep(5000);
          }
          catch(InterruptedException ie)
          {
              System.out.println(""+ie);
              //Thread.currentThread().interrupt();
          }
  
          //assuming all goes in one shot
          channel.write(ByteBuffer.wrap(message.getBytes()));
  
          key.interestOps(SelectionKey.OP_READ/*|SelectionKey.OP_WRITE*/);
      }
  
      private void connect(SelectionKey key){
          SocketChannel channel= (SocketChannel) key.channel();
  
          try
          {
              if(!(channel.finishConnect())){
                  //System.out.println("* Here *");
                  return;
              }
          }
          catch(ConnectException e){
              System.out.println("Conect Exception");
              e.printStackTrace();
              try{channel.close();}
              catch(IOException ie){ie.printStackTrace();key.cancel();return;}
              return;
          }
          catch(IOException e)
          {
              System.out.println("BP 1"+e);
              e.printStackTrace();
              try{channel.close();}
              catch(IOException ie){ie.printStackTrace();key.cancel();return;}
              return;
          }
              //channel.configureBlocking(false);
              //channel.register(selector, SelectionKey.OP_WRITE);
          key.interestOps(SelectionKey.OP_WRITE);
  
      }
  }
  </code></pre>