博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
netty11---管道
阅读量:7040 次
发布时间:2019-06-28

本文共 2980 字,大约阅读时间需要 9 分钟。

客户端:

package com.server;import java.net.Socket;public class Client {    public static void main(String[] args) throws Exception {        Socket socket = new Socket("127.0.0.1", 10101);             socket.getOutputStream().write("hello".getBytes());             socket.close();    }}

服务端:

package com.server;import java.net.InetSocketAddress;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import org.jboss.netty.bootstrap.ServerBootstrap;import org.jboss.netty.channel.ChannelPipeline;import org.jboss.netty.channel.ChannelPipelineFactory;import org.jboss.netty.channel.Channels;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 Server {    public static void main(String[] args) {        //服务类        ServerBootstrap bootstrap = new ServerBootstrap();        //boss线程监听端口,worker线程负责数据读写        ExecutorService boss = Executors.newCachedThreadPool();        ExecutorService worker = Executors.newCachedThreadPool();        //设置niosocket工厂        bootstrap.setFactory(new NioServerSocketChannelFactory(boss, worker));        //设置管道的工厂        bootstrap.setPipelineFactory(new ChannelPipelineFactory() {            @Override            public ChannelPipeline getPipeline() throws Exception {                ChannelPipeline pipeline = Channels.pipeline();                pipeline.addLast("handler1", new MyHandler1());                pipeline.addLast("handler2", new MyHandler2());                return pipeline;            }        });        bootstrap.bind(new InetSocketAddress(10101));        System.out.println("start!!!");    }}
package com.server;import org.jboss.netty.buffer.ChannelBuffer;import org.jboss.netty.channel.ChannelHandlerContext;import org.jboss.netty.channel.MessageEvent;import org.jboss.netty.channel.SimpleChannelHandler;import org.jboss.netty.channel.UpstreamMessageEvent;public class MyHandler1 extends SimpleChannelHandler {    @Override    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {        ChannelBuffer buffer = (ChannelBuffer)e.getMessage();        byte[] array = buffer.array();        String message = new String(array);        System.out.println("handler1:" + message);        //传递        ctx.sendUpstream(new UpstreamMessageEvent(ctx.getChannel(), "abc", e.getRemoteAddress()));        ctx.sendUpstream(new UpstreamMessageEvent(ctx.getChannel(), "efg", e.getRemoteAddress()));    }}
package com.server;import org.jboss.netty.channel.ChannelHandlerContext;import org.jboss.netty.channel.MessageEvent;import org.jboss.netty.channel.SimpleChannelHandler;public class MyHandler2 extends SimpleChannelHandler {    @Override    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {        String message = (String)e.getMessage();        System.out.println("handler2:" + message);    }}

 

转载地址:http://mkxal.baihongyu.com/

你可能感兴趣的文章
统计函数——汇总统计时间类数据
查看>>
精进不休 .NET 4.0 (6) - ADO.NET Data Services 1.5 新特性
查看>>
android 布局页面文件出错故障排除Exception raised during rendering: java.lang.System.arraycopy([CI[CII)V...
查看>>
熄灯问题
查看>>
引用类型参数,ref按引用传值
查看>>
基于Widnows Server 2003 SP2的系统需要新的系统准备工具
查看>>
C++ 制作 json 数据 并 传送给服务端(Server) 的 php
查看>>
如何从VS2003升级到VS2008
查看>>
Kernel内核的裁剪及移植(三)
查看>>
Oracle10g Bug 4612267 补丁安装备忘录
查看>>
我的Android开源项目:JNote
查看>>
跨线程操作UI
查看>>
关于Unity加载优化,你可能遇到这些问题
查看>>
在 Windows 7 和 Windows Server 2008 R2 上安装 Windows PowerShell 3.0
查看>>
专访IBM Power总经理 纵览Power 7新特性
查看>>
如何选购台式电脑和笔记本?购买时应注意什么
查看>>
Spring MVC基于注解来格式化数据
查看>>
mysql主从同步错误解决和Slave_IO_Running: NO
查看>>
编码问题之:java.io.UTFDataFormatException: Invalid byte 2 of 2-byte UTF-8 sequence.
查看>>
配置samba服务
查看>>