Creating a Non-Blocking Socket: requires a socket channel. : NIO Socket « Network Protocol « Java






Creating a Non-Blocking Socket: requires a socket channel.

 

import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;

public class Main {
  public static void main(String[] argv) throws Exception {
    SocketChannel sChannel = SocketChannel.open();
    sChannel.configureBlocking(false);

    sChannel.connect(new InetSocketAddress("hostName", 12345));

    while (!sChannel.finishConnect()) {
      // Do something else
    }

  }
}

   
  








Related examples in the same category

1.Non block server
2.Finger Server
3.Reading from a SocketChannel
4.Writing to a SocketChannel
5.Creating a Non-Blocking Server Socket
6.Accepting a Connection on a ServerSocketChannel
7.Using a Selector to Manage Non-Blocking Server Sockets