Nett初步使用
来源:互联网 发布:网络聊天软件有哪些 编辑:程序博客网 时间:2024/05/29 12:37
import java.net.InetSocketAddress;import java.util.concurrent.Executors;import org.jboss.netty.bootstrap.ClientBootstrap;import org.jboss.netty.bootstrap.ServerBootstrap;import org.jboss.netty.channel.Channel;import org.jboss.netty.channel.ChannelFuture;import org.jboss.netty.channel.ChannelFutureListener;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.channel.socket.nio.NioServerSocketChannelFactory;import org.jboss.netty.handler.codec.string.StringDecoder;import org.jboss.netty.handler.codec.string.StringEncoder;public class TestCase {public static void testServer(){ServerBootstrap serverBootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));serverBootstrap.setPipelineFactory(new ChannelPipelineFactory() {public ChannelPipeline getPipeline() throws Exception {ChannelPipeline channelPipleline = Channels.pipeline();channelPipleline.addLast("decoder", new StringDecoder());channelPipleline.addLast("encoder", new StringEncoder());channelPipleline.addLast("handler", new HelloServerHandler());return channelPipleline;}});serverBootstrap.bind(new InetSocketAddress(3333));}public static void testClient(){ClientBootstrap clientBootstrap = new ClientBootstrap(new NioClientSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));clientBootstrap.setPipelineFactory(new ChannelPipelineFactory() {public ChannelPipeline getPipeline() throws Exception {ChannelPipeline channelPipleline = Channels.pipeline();channelPipleline.addLast("decoder", new StringDecoder());channelPipleline.addLast("encoder", new StringEncoder());channelPipleline.addLast("handler", new HelloClientHandler());return channelPipleline;}});ChannelFuture future = clientBootstrap.connect(new InetSocketAddress("localhost", 3333));future.addListener(new ChannelFutureListener() {public void operationComplete(ChannelFuture arg0) throws Exception {System.out.println("establish connected.");}});future.getChannel().getCloseFuture().awaitUninterruptibly();clientBootstrap.releaseExternalResources();System.out.println("Client over!");}public static void main(String[] args) {testServer();testClient();}}class HelloServerHandler extends SimpleChannelHandler{@Overridepublic void messageReceived(ChannelHandlerContext ctx, MessageEvent e)throws Exception {String strMsg = (String) e.getMessage();System.out.println(strMsg);}@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)throws Exception {System.out.println(e.getCause());e.getChannel().close();}}class HelloClientHandler extends SimpleChannelHandler{@Overridepublic void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e)throws Exception {String strMsg = null;for(int i = 0; true; i++){strMsg = "hello" + i;e.getChannel().write(strMsg);Thread.sleep(1000);}}@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)throws Exception {System.out.println(e.getCause());e.getChannel().close();}}
0 0
- Nett初步使用
- asp.nett网站发布过程
- ASP.NETt与SQL一起安装部署
- toms sko på nett vmfnli:u
- ASP.NETt运行原理和运行机制
- Makefile使用初步
- [转载]Makefile使用初步
- Makefile使用初步
- Makefile使用初步
- 初步使用STL
- Ajax使用初步
- Makefile使用初步
- TreeView使用初步
- FCKeditor使用初步
- C#哈西表使用初步
- gdb使用初步
- OpenCms 使用初步印象
- Ajax使用初步
- 递归思想(一)
- 转-如何读懂源码(下)
- 谈一下我对于指针的理解 .
- C++中extern “C”含义深层探索
- Spring 基础语义
- Nett初步使用
- 问题2:
- 希尔排序Shell sort
- IOS面试题--002
- ios7学习之路六(隐藏状态栏 )
- 文科出身也能获得图灵奖
- 转-Spring AOP 术语解释(最容易理解)
- poj 1321 棋盘问题(DFS)
- IOS面试题--003 .