Erlang hello world server
The source code is available at yufeng’s blog, see http://blog.yufeng.info/archives/105
Just copy the code after “cat ehttpd.erl”, and compile it.
$ erlc ehttpd.erl
$ taskset -c 1 erl +K true +h 99999 +P 99999 -smp enable +S 2:1 -s ehttpd
$ taskset -c 1-7 erl +K true -s ehttpd
We use taskset to limit erlang vm to use only 1 CPU/core or use all CPU cores. The 2nd line is run in single CPU mode, and the 3rd line is run in multi-core CPU mode.
Java source code, save the 2 class as HttpServer.java and HttpProtocolHandler.java, and do necessary import.
public class HttpServer {
public static void main(String[] args) throws Exception {
SocketAcceptor acceptor = new NioSocketAcceptor(4);
acceptor.setReuseAddress( true );
int port = 8080;
String hostname = null;
if (args.length > 1) {
hostname = args[0];
port = Integer.parseInt(args[1]);
}
// Bind
acceptor.setHandler(new HttpProtocolHandler());
if (hostname != null)
acceptor.bind(new InetSocketAddress(hostname, port));
else
acceptor.bind(new InetSocketAddress(port));
System.out.println("Listening on port " + port);
Thread.currentThread().join();
}
}
public class HttpProtocolHandler extends IoHandlerAdapter {
public void sessionCreated(IoSession session) {
session.getConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
session.setAttribute(SslFilter.USE_NOTIFICATION);
}
public void sessionClosed(IoSession session) throws Exception {}
public void sessionOpened(IoSession session) throws Exception {}
public void sessionIdle(IoSession session, IdleStatus status) {}
public void exceptionCaught(IoSession session, Throwable cause) {
session.close(true);
}
static IoBuffer RESULT = null;
public static String HTTP_200 = "HTTP/1.1 200 OK\r\nContent-Length: 13\r\n\r\n" +
"hello world\r\n";
static {
RESULT = IoBuffer.allocate(32).setAutoExpand(true);
RESULT.put(HTTP_200.getBytes());
RESULT.flip();
}
public void messageReceived(IoSession session, Object message)
throws Exception {
if (message instanceof IoBuffer) {
IoBuffer buf = (IoBuffer) message;
int c = buf.get();
if (c == 'G' || c == 'g') {
session.write(RESULT.duplicate());
}
session.close(false);
}
}
}