只是代码,建议配合http://ifeve.com/netty5-user-guide/此网站观看
package com.demo.netty;
import org.junit.Before;
import org.junit.Test;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
public class Server {
private int port;
@Before
public void init() {
this.port = 8088;
}
@Test
public void run() throws Exception {
NioEventLoopGroup bossGroup = new NioEventLoopGroup(); //用来接收进来的连接
NioEventLoopGroup wokerGroup = new NioEventLoopGroup();//用来处理已经被接收的连接,boss接收到连接后就会把连接信息注册到woker上,由woker去做具体处理
ServerBootstrap serverBootstrap = new ServerBootstrap();//Netty服务端启动Nio的辅助启动类
serverBootstrap.group(bossGroup,wokerGroup).channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new ServerHandler());
}
}).option(ChannelOption.SO_BACKLOG, 1024).childOption(ChannelOption.SO_KEEPALIVE, true)
.option(ChannelOption.SO_SNDBUF, 3*1024).option(ChannelOption.SO_RCVBUF, 3*1024);
ChannelFuture sync = serverBootstrap.bind(port).sync();
sync.channel().closeFuture().sync();
bossGroup.shutdownGracefully();
wokerGroup.shutdownGracefully();
}
}
package com.demo.netty;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
public class ServerHandler extends ChannelHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf b = (ByteBuf)msg;
byte [] by = new byte[b.readableBytes()];
b.readBytes(by);
System.out.println(new String(by));
ByteBuf buffer = ctx.alloc().buffer(50);
buffer.writeBytes("Hello Man".getBytes());
ctx.writeAndFlush(buffer);
}
}
package com.demo.netty;
import org.junit.Before;
import org.junit.Test;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
public class Client {
private int port;
@Before
public void init() {
this.port=8088;
}
@Test
public void run() throws Exception {
NioEventLoopGroup workerGroup = new NioEventLoopGroup();//客户端线程只需要指定一个,因为我们只要负责连接服务端就行
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(workerGroup).channel(NioSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, true)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new ClientHandler());
}
});
ChannelFuture sync = bootstrap.connect("127.0.0.1",port).sync();
sync.channel().writeAndFlush(Unpooled.copiedBuffer("Hello Netty5".getBytes()));
sync.channel().closeFuture().sync();
}
}
package com.demo.netty;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.ReferenceCountUtil;
public class ClientHandler extends ChannelHandlerAdapter{
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf b = (ByteBuf)msg;
try {
byte[] byt = new byte[b.readableBytes()];
b.readBytes(byt);
System.out.println(new String(byt));
}finally {
System.out.println("清除之前 "+b.readableBytes());
b.release();
System.out.println("清除之后 "+b.readableBytes());
}
}
}