Created
June 23, 2012 11:26
-
-
Save purplefox/2977949 to your computer and use it in GitHub Desktop.
Server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Server { | |
public static void main(String[] args) { | |
try { | |
Executor pool = Executors.newCachedThreadPool(); | |
ChannelFactory factory = | |
new NioServerSocketChannelFactory( | |
pool, | |
pool); | |
ServerBootstrap bootstrap = new ServerBootstrap(factory); | |
bootstrap.setOption("backlog", 10000); | |
bootstrap.setPipelineFactory(new ChannelPipelineFactory() { | |
public ChannelPipeline getPipeline() { | |
ChannelPipeline pipeline = Channels.pipeline(); | |
pipeline.addLast("handler", new ServerHandler()); | |
return pipeline; | |
} | |
}); | |
Channel serverChannel = bootstrap.bind(new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 1234)); | |
System.out.println("Listening"); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
private static class ServerHandler extends SimpleChannelHandler { | |
@Override | |
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) { | |
System.out.println("Channel connected"); | |
} | |
@Override | |
public void channelInterestChanged(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { | |
System.out.println("Interest changed"); | |
} | |
@Override | |
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) { | |
System.out.println("Channel closed"); | |
} | |
@Override | |
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) { | |
System.out.println("Message received"); | |
} | |
@Override | |
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) { | |
System.out.println("Exception caught"); | |
e.getCause().printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment