Nett初步使用

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.Channel;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelFutureListener;
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;



public class TestCase {

	public static void testServer(){
		ServerBootstrap serverBootstrap = new ServerBootstrap(
				new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), 
						Executors.newCachedThreadPool()));
		
		serverBootstrap.setPipelineFactory(new ChannelPipelineFactory() {			
			public ChannelPipeline getPipeline() throws Exception {
				ChannelPipeline channelPipleline = Channels.pipeline();
				channelPipleline.addLast("decoder", new StringDecoder());
				channelPipleline.addLast("encoder", new StringEncoder());
				channelPipleline.addLast("handler", new HelloServerHandler());
				return channelPipleline;
			}
		});
		
		serverBootstrap.bind(new InetSocketAddress(3333));

	}

	public static void testClient(){
		ClientBootstrap clientBootstrap = new ClientBootstrap(
				new NioClientSocketChannelFactory(Executors.newCachedThreadPool(), 
						Executors.newCachedThreadPool()));
		
		clientBootstrap.setPipelineFactory(new ChannelPipelineFactory() {			
			public ChannelPipeline getPipeline() throws Exception {
				ChannelPipeline channelPipleline = Channels.pipeline();
				channelPipleline.addLast("decoder", new StringDecoder());
				channelPipleline.addLast("encoder", new StringEncoder());
				channelPipleline.addLast("handler", new HelloClientHandler());
				return channelPipleline;
			}
		});
		
		ChannelFuture future = clientBootstrap.connect(new InetSocketAddress("localhost", 3333));
		future.addListener(new ChannelFutureListener() {
			
			public void operationComplete(ChannelFuture arg0) throws Exception {
				System.out.println("establish connected.");
			}
		});

		future.getChannel().getCloseFuture().awaitUninterruptibly();
		clientBootstrap.releaseExternalResources();
		System.out.println("Client over!");
	}
	
	public static void main(String[] args) {
		testServer();
		testClient();
	}
}


class HelloServerHandler extends SimpleChannelHandler{
	@Override
	public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
			throws Exception {
		String strMsg = (String) e.getMessage();
		System.out.println(strMsg);
	}	
	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
			throws Exception {
		System.out.println(e.getCause());
		e.getChannel().close();
	}
}


class HelloClientHandler extends SimpleChannelHandler{
	@Override
	public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e)
			throws Exception {
		String strMsg = null;
		for(int i = 0; true; i++){
			strMsg = "hello" + i;
			e.getChannel().write(strMsg);
			Thread.sleep(1000);
		}
	}
	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
			throws Exception {
		System.out.println(e.getCause());
		e.getChannel().close();
	}
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值