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();    
    }    
}    

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

贴个流程:


后面就是事件了:


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


write:NioWorker.write()


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值