服务端:
package Demo3;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
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.nio.NioServerSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
public class NettyServer5 {
/**Netty5.0的服务端
* @param args
*/
public static void main(String[] args) {
//服务类
ServerBootstrap bootstrap = new ServerBootstrap();
EventLoopGroup boss = new NioEventLoopGroup();
EventLoopGroup worker = new NioEventLoopGroup();
try {
//设置线程池
bootstrap.group(boss,worker);
//设置socket工厂
bootstrap.channel(NioServerSocketChannel.class);
bootstrap.childHandler(new ChannelInitializer<Channel>() {
protected void initChannel(Channel ch) throws Exception {
ch.pipeline().addLast(new StringDecoder());
ch.pipeline().addLast(new StringEncoder());
ch.pipeline().addLast(new HelloHandler());
};
});
//tcp设置参数
bootstrap.option(ChannelOption.SO_BACKLOG, 2048);//连接缓冲区的大小
bootstrap.option(ChannelOption.SO_KEEPALIVE, true);//维持链接的活跃,清除死连接
bootstrap.option(ChannelOption.TCP_NODELAY, true);//关闭延迟发送
//绑定端口
ChannelFuture future = bootstrap.bind(10100);
System.out.println("start");
//等待服务端关闭
future.channel().closeFuture().sync();
} catch (Exception e) {
// TODO: handle exception
}finally{
boss.shutdownGracefully();
worker.shutdownGracefully();
}
}
}
package Demo3;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
public class HelloHandler extends SimpleChannelInboundHandler<String>{
/**
* 消息处理
*/
@Override
protected void messageReceived(ChannelHandlerContext ctx, String msg)
throws Exception {
// TODO Auto-generated method stub
System.out.println(msg);
ctx.channel().writeAndFlush("server: hello client !!!");//ctx.writeAndFlush("hi");
}
}
客户端:
package Demo4;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
public class NettyClient5 {
/**
* @param args
*/
public static void main(String[] args) {
Bootstrap bootstrap = new Bootstrap();
EventLoopGroup worker = new NioEventLoopGroup();
try {
//设置线程池
bootstrap.group(worker);
//设置socket工厂
bootstrap.channel(NioSocketChannel.class);
//设置管道
bootstrap.handler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
// TODO Auto-generated method stub
ch.pipeline().addLast(new StringDecoder());
ch.pipeline().addLast(new StringEncoder());
ch.pipeline().addLast(new HiHandler());
}
});
ChannelFuture connect = bootstrap.connect("127.0.0.1", 10100);
BufferedReader bufferReader = new BufferedReader(new InputStreamReader(System.in));
while(true)
{
System.out.println("please input :");
String msg = bufferReader.readLine();
connect.channel().writeAndFlush(msg);
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
package Demo4;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
public class HiHandler extends SimpleChannelInboundHandler<String>{
@Override
protected void messageReceived(ChannelHandlerContext ctx, String msg)
throws Exception {
System.out.println("client msg:"+msg);
}
}
两个小例子,看看,Netty5.0+和Netty3.0+这两个版本的差别还是挺大的,在封装上,netty5.0+复杂了很多,继续研究