1.Netty
netty是JBOSS针对网络开发的一套应用框架,它也是在NIO的基础上发展起来的。netty基于异步的事件驱动,具有高性能、高扩展性等特性,它提供了统一的底层协议接口,使得开发者从底层的网络协议(比如 TCP/IP、UDP)中解脱出来.
客户端
<p>package com.kkTest.netty;</p><p>import java.net.InetSocketAddress; import java.util.concurrent.Executors;</p><p>import org.jboss.netty.bootstrap.ClientBootstrap; import org.jboss.netty.channel.ChannelHandlerContext; import org.jboss.netty.channel.ChannelPipeline; import org.jboss.netty.channel.ChannelPipelineFactory; import org.jboss.netty.channel.ChannelStateEvent; import org.jboss.netty.channel.Channels; import org.jboss.netty.channel.ExceptionEvent; import org.jboss.netty.channel.MessageEvent; import org.jboss.netty.channel.SimpleChannelHandler; import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory; import org.jboss.netty.handler.codec.string.StringDecoder; import org.jboss.netty.handler.codec.string.StringEncoder;</p><p>/** * Netty 客户端 * * @author kuanghj * */ public class KKTestClient {</p><p> public static void main(String args[]) { // Client服务启动器 ClientBootstrap bootstrap = new ClientBootstrap( new NioClientSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool())); // 设置一个处理服务端消息和各种消息事件的类(Handler) bootstrap.setPipelineFactory(new ChannelPipelineFactory() { public ChannelPipeline getPipeline() throws Exception { // 传输为字符串类型 ChannelPipeline pipeline = Channels.pipeline(); pipeline.addLast("encode",new StringEncoder()); pipeline.addLast("decode",new StringDecoder()); pipeline.addLast("handler",new KKTestClientHandler()); return pipeline; } }); bootstrap.setOption("tcpNoDelay" , true); bootstrap.setOption("keepAlive", true); // 连接到本地的8800端口的服务端 bootstrap.connect(new InetSocketAddress("10.10.113.243", 8800)); }</p><p> /** * 事件处理 * @author kuanghj * */ private static class KKTestClientHandler extends SimpleChannelHandler {</p><p> /** * 当绑定到服务端的时候触发,打印"Hello world, I'm client." * * @author kuanghj */ @Override public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) { System.out.println("Hello world, I'm client."); e.getChannel().write("abcd"); } @Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) { e.getChannel().close(); System.out.println(e.getMessage()); } @Override public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) { e.getCause().printStackTrace(); e.getChannel().close(); } } }</p>
服务端
<p>package com.kkTest.netty;</p><p>import java.net.InetSocketAddress; import java.util.concurrent.Executors;</p><p>import org.jboss.netty.bootstrap.ServerBootstrap; import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.ChannelHandlerContext; import org.jboss.netty.channel.ChannelPipeline; import org.jboss.netty.channel.ChannelPipelineFactory; import org.jboss.netty.channel.ChannelStateEvent; import org.jboss.netty.channel.Channels; import org.jboss.netty.channel.ExceptionEvent; import org.jboss.netty.channel.MessageEvent; import org.jboss.netty.channel.SimpleChannelHandler; import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; import org.jboss.netty.handler.codec.string.StringDecoder; import org.jboss.netty.handler.codec.string.StringEncoder;</p><p> /** * Netty 服务端 * * @author kuanghj * */ public class KKTestServer {</p><p> public static void main(String args[]) { // Server服务启动器 ServerBootstrap bootstrap = new ServerBootstrap( new NioServerSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool())); // 设置一个处理客户端消息和各种消息事件的类(Handler) bootstrap.setPipelineFactory(new ChannelPipelineFactory() { public ChannelPipeline getPipeline() throws Exception { ChannelPipeline pipeline = Channels.pipeline(); pipeline.addLast("encode",new StringEncoder()); pipeline.addLast("decode",new StringDecoder()); pipeline.addLast("handler",new KKTestServerHandler()); return pipeline; } }); // 开放8000端口供客户端访问。 bootstrap.bind(new InetSocketAddress(8800)); }</p><p> private static class KKTestServerHandler extends SimpleChannelHandler {</p><p> /** * 当有客户端绑定到服务端的时候触发,打印"Hello world, I'm server." * * @author kuanghj */ @Override public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) { System.out.println("Hello world, I'm server."); } @Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent message) throws Exception { System.out.println("received message 1:"+message.getMessage()); message.getChannel().write("received "+message.getMessage()); } @Override public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) { e.getCause().printStackTrace(); Channel ch = e.getChannel(); ch.close(); } } }</p>
连接时会 各自会有输出,当客户端有写入时 在服务端 messageReceived 会接收到