Read Web Pages with Nonblocking SocketChannel in Java
Description
The following code shows how to read Web Pages with Nonblocking SocketChannel.
Example
//from w w w. j a v a 2 s .c om
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.util.Iterator;
import java.util.Set;
public class Main {
static Selector selector;
public static void main(String args[]) throws Exception {
int slashPos;
String resource = "www.java2s.com/index.htm"; // skip HTTP://
slashPos = resource.indexOf('/'); // find host/file separator
if (slashPos < 0) {
resource = resource + "/";
slashPos = resource.indexOf('/');
}
String file = resource.substring(slashPos); // isolate host and file parts
String host = resource.substring(0, slashPos);
System.out.println("Host to contact: '" + host + "'");
System.out.println("File to fetch : '" + file + "'");
Charset charset = Charset.forName("ISO-8859-1");
CharsetDecoder decoder = charset.newDecoder();
CharsetEncoder encoder = charset.newEncoder();
ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
CharBuffer charBuffer = CharBuffer.allocate(1024);
InetSocketAddress socketAddress = new InetSocketAddress(host, 80);
SocketChannel channel = SocketChannel.open();
channel.configureBlocking(false);
channel.connect(socketAddress);
selector = Selector.open();
channel.register(selector, SelectionKey.OP_CONNECT | SelectionKey.OP_READ);
while (selector.select(500) > 0) {
Set readyKeys = selector.selectedKeys();
Iterator readyItor = readyKeys.iterator();
while (readyItor.hasNext()) {
SelectionKey key = (SelectionKey) readyItor.next();
readyItor.remove();
SocketChannel keyChannel = (SocketChannel) key.channel();
if (key.isConnectable()) {
if (keyChannel.isConnectionPending()) {
keyChannel.finishConnect();
}
String request = "GET " + file + " \r\n\r\n";
keyChannel.write(encoder.encode(CharBuffer.wrap(request)));
} else if (key.isReadable()) {
keyChannel.read(buffer);
buffer.flip();
decoder.decode(buffer, charBuffer, false);
charBuffer.flip();
System.out.print(charBuffer);
buffer.clear();
charBuffer.clear();
} else {
System.err.println("Unknown key");
}
}
}
channel.close();
System.out.println("\nDone.");
}
}
The code above generates the following result.