Netty学习

1.Dependency:

		<dependency>
			<groupId>io.netty</groupId>
			<artifactId>netty-all</artifactId>
			<version>4.1.0.CR7</version>
		</dependency>

2.单服务器,多客户端例子:

2.1.创建一个消息处理的Handler,它继承ChannelInboundHandlerAdapter

package leo.test;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;

/**
 * @author Leo
 * @date 2016年4月25日下午2:41:52
 * @description
 * @usage
 */
public class MsgHandler extends ChannelInboundHandlerAdapter {
	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) { // (2)
		ByteBuf in = (ByteBuf) msg;//接收
		ByteBuf out = ctx.alloc().buffer();//发送
		System.out.println("Rcv:" + in.toString(CharsetUtil.UTF_8));//打印接收到的消息
		out.writeBytes("Server have rcv the msg.".getBytes(CharsetUtil.UTF_8));//将要发送的消息以byte形式存在out中
		ctx.writeAndFlush(out);//通过ctx输出到接收端.
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { // (4)
		// Close the connection when an exception is raised.
		cause.printStackTrace();
		ctx.close();
	}
}
2.2.根据官网的demo创建一个Server

package leo.test;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * @author Leo
 * @date 2016年4月25日下午2:43:49
 * @description
 * @usage
 */
public class Server {
	private int port;

	public Server(int port) {
		this.port = port;
	}

	public void run() throws Exception {
		EventLoopGroup bossGroup = new NioEventLoopGroup(); // (1)
		EventLoopGroup workerGroup = new NioEventLoopGroup();
		try {
			ServerBootstrap b = new ServerBootstrap(); // (2)
			b.group(bossGroup, workerGroup)
					.channel(NioServerSocketChannel.class) // (3)
					.childHandler(new ChannelInitializer<SocketChannel>() { // (4)
								@Override
								public void initChannel(SocketChannel ch)
										throws Exception {
									ch.pipeline().addLast(new MsgHandler());
								}
							}).option(ChannelOption.SO_BACKLOG, 128) // (5)
					.childOption(ChannelOption.SO_KEEPALIVE, true); // (6)

			// Bind and start to accept incoming connections.
			ChannelFuture f = b.bind(port).sync(); // (7)

			// Wait until the server socket is closed.
			// In this example, this does not happen, but you can do that to
			// gracefully
			// shut down your server.
			f.channel().closeFuture().sync();
		} finally {
			workerGroup.shutdownGracefully();
			bossGroup.shutdownGracefully();
		}
	}

}


2.3.测试:
2.3.1.先启动server

package leo.test;

import org.junit.Test;

/**
 * @author Leo
 * @date 2016年4月26日上午9:29:16
 * @description
 * @usage
 */
public class MyTest {
	@Test
	public void test01() {
		try {
			new Server(9527).run();//port number:9527
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
2.3.2.如果是在window下,你可以很方便的用telnet进行测试.

2.3.2.1.进入cmd

2.3.2.2.输入    telnet localhost 9527
2.3.2.3.随便输入点东西,就可以看到客户端与服务器的交互了.


参考资料:http://netty.io/wiki/user-guide-for-4.x.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值