SocketChannel Asynchronous

In this chapter you will learn:

  1. Asynchronous connection of a SocketChannel

Asynchronous connection of a SocketChannel

import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;
/*from jav a  2 s .  co m*/

public class MainClass {
  public static void main(String[] argv) throws Exception {
    String host = "localhost";
    int port = 80;

    InetSocketAddress addr = new InetSocketAddress(host, port);
    SocketChannel sc = SocketChannel.open();

    sc.configureBlocking(false);

    System.out.println("initiating connection");

    sc.connect(addr);

    while (!sc.finishConnect()) {
      System.out.println("doing something useless");
    }

    System.out.println("connection established");

    sc.close();
  }

}

Next chapter...

What you will learn in the next chapter:

  1. New IO Hello Server
  2. Accepting a Connection on a ServerSocketChannel
  3. non-blocking accept() using ServerSocketChannel
Home » Java Tutorial » Socket

Socket

    Socket
    Socket creation
    Socket read
    HTTP client
    SMTP Client
    Cipher Socket
    Socket connection and thread

ServerSocket

    ServerSocket
    ServerSocket connection
    File server and client
    Thread based Server
    Time server
    SocketServer based zip server
    ServerSocketChannel
    Channel selector

SocketChannel

    SocketChannel Creation
    Read and write with SocketChannel
    SocketChannel based HTTP client
    SocketChannel Asynchronous

ServerSocketChannel

    ServerSocketChannel
    Channel selector

SSL

    SSL Client Socket
    SSL Server Socket

UDP

    DatagramSocket
    DatagramChannel
    Multicast Group

Cookie

    Cookies