Java NIO框架Netty1简单发送接受

先来个例子:再慢慢开源,走流程了!!!

1,Server端:

package com.netty1;

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 Server {    
    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 p = Channels.pipeline();    
                p.addLast("encode",new StringEncoder());    
                p.addLast("decode",new StringDecoder());    
                p.addLast("handler",new ServerHandler());    
                return p;    
            }    
        });    
        bootstrap.setOption("child.tcpNoDelay", true);    
        bootstrap.setOption("child.keepAlive", true); 
        //--服务端监听端口号; 
        bootstrap.bind(new InetSocketAddress(14000));  
    }    
}    

2.ServerHandler

package com.netty1;

import org.jboss.netty.channel.*;    
      
public class ServerHandler 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();   
    }   
}  

3,Client端

package com.netty1;

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 Client {    
    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 p = Channels.pipeline();    
                p.addLast("encode",new StringEncoder());    
                p.addLast("decode",new StringDecoder());    
                p.addLast("handler",new ClientHandler());    
                return p;    
            }    
        });    
        bootstrap.setOption("tcpNoDelay" , true);    
        bootstrap.setOption("keepAlive", true);    
        bootstrap.connect (new InetSocketAddress("127.0.0.1", 14000));    
    }    
}    

4,ClientHandler

package com.netty1;

import org.jboss.netty.channel.*;    
    
    
public class ClientHandler 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();    
    }    
}    

简单的客户端,服务端代码算是搞定了,运行!!!

贴个流程:


后面就是事件了:

Java NIO框架Netty1简单发送接受_第1张图片

实际Client的调用时这样的:这个时候才到自己的ClientHandler


write:NioWorker.write()


你可能感兴趣的:(Java NIO框架Netty1简单发送接受)