Netty例子.

2 篇文章 0 订阅
	//服务端程序.
    package netty;  
      
    import org.jboss.netty.bootstrap.ServerBootstrap;  
    import org.jboss.netty.channel.ChannelFactory;  
    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;  
      
    import java.net.InetSocketAddress;  
    import java.util.concurrent.Executors;  
       
	public class DiscardServer {  
        public static void main(String[] args) throws Exception {  
            ChannelFactory factory = new NioServerSocketChannelFactory(  
                Executors.newCachedThreadPool(),  
                Executors.newCachedThreadPool());  
            ServerBootstrap bootstrap = new ServerBootstrap (factory);  
            bootstrap.setPipelineFactory(new ChannelPipelineFactory() {  
                public ChannelPipeline getPipeline() {  
                     ChannelPipeline pipeline = Channels.pipeline();  
                    pipeline.addLast("encode",new StringEncoder());  
                    pipeline.addLast("decode",new StringDecoder());  
                    pipeline.addLast("handler",new DiscardServerHandler());  
                    return pipeline;  
                }  
            });  
            bootstrap.setOption("child.tcpNoDelay", true);  
            bootstrap.setOption("child.keepAlive", true);  
            bootstrap.bind(new InetSocketAddress(8080));  --监听端口号;
        }  
    }  

    package netty;  
      
    import org.jboss.netty.buffer.ChannelBuffer;  
    import org.jboss.netty.buffer.ChannelBuffers;  
    import org.jboss.netty.channel.*;  
        
    public class DiscardServerHandler extends SimpleChannelUpstreamHandler  {  
        @Override  
        public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {  
           System.out.println("服务器接收1:"+e.getMessage());  
        }  
       

       @Override  
        public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) {  
            e.getChannel().write("Reply");  
        }

 
		@Override 
		public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) { 
		e.getCause().printStackTrace(); 
		Channel ch = e.getChannel(); 
		ch.close(); 
		} 
	}

//客户端程序.
    package netty;  
      
    import org.jboss.netty.bootstrap.ClientBootstrap;  
    import org.jboss.netty.channel.ChannelFactory;  
    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.NioClientSocketChannelFactory;  
    import org.jboss.netty.handler.codec.string.StringDecoder;  
    import org.jboss.netty.handler.codec.string.StringEncoder;  
      
    import java.net.InetSocketAddress;  
    import java.util.concurrent.Executors;  
       
    public class TimeClient {  
        public static void main(String[] args) throws Exception {  
              
            ChannelFactory factory = new NioClientSocketChannelFactory(  
                Executors.newCachedThreadPool(),  
                Executors.newCachedThreadPool());  
            ClientBootstrap bootstrap = new ClientBootstrap(factory);  
            bootstrap.setPipelineFactory(new ChannelPipelineFactory() {  
                public ChannelPipeline getPipeline() {  
                    ChannelPipeline pipeline = Channels.pipeline();  
                    pipeline.addLast("encode",new StringEncoder());  
                    pipeline.addLast("decode",new StringDecoder());  
                    pipeline.addLast("handler",new TimeClientHandler());  
                    return pipeline;  
                }  
            });  
            bootstrap.setOption("tcpNoDelay" , true);  
            bootstrap.setOption("keepAlive", true);  
            bootstrap.connect (new InetSocketAddress("127.0.0.1", 8080));  
        }  
    }  

    package netty;  
       
    import org.jboss.netty.buffer.ChannelBuffer;  
    import org.jboss.netty.buffer.ChannelBuffers;  
    import org.jboss.netty.channel.*;  
      
    import java.util.Date;  
      
      
    public class TimeClientHandler extends SimpleChannelUpstreamHandler  {  
        @Override  
        public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) {  
            e.getChannel().write("request");  
        }  
      
        @Override  
        public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {  
            System.out.println("服务器回复:" + e.getMessage());
            e.getChannel().close();  
        }  
          
        @Override  
        public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {  
            e.getCause().printStackTrace();  
            e.getChannel().close();  
        }  
    }  


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值