1.关于netty可以自行google,我先概述一下netty入门helloword 只要按步骤写 入门后就可自己专研源码
用maven构建:
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>5.0.0.Alpha2</version>
</dependency>
首先建立服务器端程序:
public class Mserver {
public static void main(String[] args) throws Exception{
EventLoopGroup boss = new NioEventLoopGroup();
EventLoopGroup worker = new NioEventLoopGroup();
/**
* 服务器端定义两个线程组 boss接收来自客服端信息然后转给worker处理
*/
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();//辅助类建立通道
serverBootstrap.group(boss,worker).channel(NioServerSocketChannel.class)
.childHandler(new MyserverInitializer());
ChannelFuture channelFuture = serverBootstrap.bind(9999).sync();
channelFuture.channel().closeFuture().sync();
}finally {
boss.shutdownGracefully();
worker.shutdownGracefully();
}
}
}
在自定义handler类
public class MyServerHandler extends SimpleChannelInboundHandler<String> {
protected void messageReceived(ChannelHandlerContext channelHandlerContext, String s) throws Exception {
System.out.println(channelHandlerContext.channel().remoteAddress() +" news client to server" +s);//
channelHandlerContext.writeAndFlush(" server to client" + new Date());
}
/**
*字符串s是client发给server的信息
*channelHandlerContext.writeAndFlush()获取通道然后发消息给client
*/
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}
将handler绑定到pipline
public class MyserverInitializer extends ChannelInitializer<SocketChannel> {
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline pipeline = socketChannel.pipeline();
pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));
pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8));
pipeline.addLast(new MyServerHandler());//绑定自定义handler类
}
同理客户端类似的写三个类
public class MyClient {
public static void main(String[] args) throws Exception{
EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class)
.handler(new netty_HelloWorld.MyClientInitializer());
ChannelFuture channelFuture = bootstrap.bind("localhost",9999).sync();
channelFuture.channel().closeFuture().sync();
}finally {
eventLoopGroup.shutdownGracefully();
}
}
}
public class MyClientHandler extends SimpleChannelInboundHandler<String> {
protected void messageReceived(ChannelHandlerContext channelHandlerContext, String s) throws Exception {
System.out.println(channelHandlerContext.channel().remoteAddress() +" news server to client" +s);//
channelHandlerContext.writeAndFlush(" client to server " + UUID.randomUUID());
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
ctx.writeAndFlush("来自客户端开启消息大门");
}//只有重写这个类客户端才会开始想服务器端发送消息
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}//抛出异常 必须重载
}
public class MyClientInitializer extends ChannelInitializer<SocketChannel>{
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline pipeline = socketChannel.pipeline();
pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));
pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8));
pipeline.addLast(new MyClientHandler());//自定义handler放在最后
}
}
建议边看边google查询先关关系,弄懂以上之后你就开始学习各种handler类了
之后查看源码看多线程机制
代码见github:https://github.com/anglesun/netty/tree/master