SocketChannel Asynchronous
In this chapter you will learn:
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:
- New IO Hello Server
- Accepting a Connection on a ServerSocketChannel
- non-blocking accept() using ServerSocketChannel
Home » Java Tutorial » Socket