netty 入门例子

package www.teamtopgame.com.netty;

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.ChannelFuture;
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;

class HelloWorldServerHandler extends SimpleChannelHandler {

	public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e)
			throws Exception {
              //往Channel写入信息。
               e.getChannel().write("Hello, World,i'm server");
	}

	public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
		System.out.println("Unexpected exception from downstream."
				+ e.getCause());
		e.getChannel().close();
	}
}

class HelloWorldClientHandler extends SimpleChannelHandler {

	public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
              //接受信息,并输出。
               String message = (String) e.getMessage();
		System.out.println(message);
		e.getChannel().close();
	}

	public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
		System.out.println("Unexpected exception from downstream."
				+ e.getCause());
		e.getChannel().close();
	}
}

/**
 * Netty VS MinaNetty基于Pipeline处理,Mina基于Filter过滤 Netty的事件驱动模型具有更好的扩展性和易用性
 * Https,SSL,PB,RSTP,Text &Binary等协议支持 Netty中UDP传输有更好的支持官方测试Netty比Mina性能更好
 * 
 * @author Administrator
 * 
 */
public class TestCase {

	public static void testServer() {
		// 启动server服务端。。
		System.out.println("server...启动");
		ServerBootstrap bootstrap = new ServerBootstrap(
				new NioServerSocketChannelFactory(
						Executors.newCachedThreadPool(),
						Executors.newCachedThreadPool()));
		
		bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
			public ChannelPipeline getPipeline() {
				ChannelPipeline pipeline = Channels.pipeline();
				pipeline.addLast("decoder", new StringDecoder());  //声明解码的类,为StringDecoder.
				pipeline.addLast("encoder", new StringEncoder());  //声明编码的类,为StringEncoder.
				pipeline.addLast("handler", new HelloWorldServerHandler());
				return pipeline;
			}
		});
		// 开放8080端口给客户端
		bootstrap.bind(new InetSocketAddress(8080));
	}

	public static void testClient() {
		// client端
		System.out.println("client...启动");
		ClientBootstrap bootstrap = new ClientBootstrap(
				new NioClientSocketChannelFactory(
						Executors.newCachedThreadPool(),
						Executors.newCachedThreadPool()));
		// It means one same HelloWorldClientHandler instance is going to handle
		// multiple Channels and consequently the data will be corrupted.
		// 基于上面这个描述,必须用到ChannelPipelineFactory每次创建一个pipeline
		bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
			public ChannelPipeline getPipeline() {
				ChannelPipeline pipeline = Channels.pipeline();
				pipeline.addLast("decoder", new StringDecoder());
				pipeline.addLast("encoder", new StringEncoder());
				pipeline.addLast("handler", new HelloWorldClientHandler());
				return pipeline;
			}
		});
		// 创建无连接传输channel的辅助类(UDP),包括client和server
		//连接到server指定的端口,完成绑定。
		ChannelFuture future = bootstrap.connect(new InetSocketAddress(
				"localhost", 8080));
		future.getChannel().getCloseFuture().awaitUninterruptibly();
		bootstrap.releaseExternalResources();
	}


	public static void main(String[] args) {
		testServer();
		testClient();
	}

}

输出结果是:

server...启动
client...启动
hello ,world,i'm client
Hello, World,i'm server

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值