1.服务端
package cn.sjxu.netty;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
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;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
/**
* @Author sjxu
* @Date 2018/4/23 20:50
* @Version 1.0
*/
public class NettyServer {
public void start() throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
//TCP方式
.channel(NioServerSocketChannel.class)
//设置TCP缓冲大小
.option(ChannelOption.SO_BACKLOG, 1024)
//设置发送缓冲大小
.option(ChannelOption.SO_SNDBUF, 32 * 1024)
//设置接受缓冲大小
.option(ChannelOption.SO_RCVBUF, 32 * 1024)
//保持连接
.option(ChannelOption.SO_KEEPALIVE, true)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
//设置分隔符
ByteBuf buf = Unpooled.copiedBuffer("end".getBytes());
ch.pipeline()
//设置分隔符
.addLast(new DelimiterBasedFrameDecoder(1024, buf))
.addLast(new StringDecoder())
.addLast(new NettyServerHandler());
}
});
//启动服务器
ChannelFuture cf = b.bind(8888).sync();
//服务器断开连接前阻塞
cf.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
//关闭所有线程
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
public static void main(String[] args) throws Exception {
new NettyServer().start();
}
}
2.服务端Handler
package cn.sjxu.netty;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
/**
* @Author sjxu
* @Date 2018/4/23 20:51
* @Version 1.0
*/
public class NettyServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println(msg);
//发送
ctx.writeAndFlush(Unpooled.copiedBuffer("收到了!!!".getBytes()))
//监听,发送后关闭连接
.addListener(ChannelFutureListener.CLOSE);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
//异常时关闭
ctx.close();
}
}
3.客户端
package cn.sjxu.netty;
import io.netty.bootstrap.Bootstrap;
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.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
/**
* @Author sjxu
* @Date 2018/4/23 21:10
* @Version 1.0
*/
public class NettyClient {
private final static String host = "127.0.0.1";
private final static int port = 8888;
public void start() throws Exception {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group)
//TCP方式
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline()
.addLast(new StringDecoder())
.addLast(new NettyClientHandler());
}
});
//连接服务器
ChannelFuture cf = b.connect(host, port).sync();
//连接断开前阻塞
cf.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
//关闭所有线程
group.shutdownGracefully().sync();
}
}
public static void main(String[] args) throws Exception {
new NettyClient().start();
}
}
4.客户端Handler
package cn.sjxu.netty;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
/**
* @Author sjxu
* @Date 2018/4/23 21:11
* @Version 1.0
*/
public class NettyClientHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
ctx.writeAndFlush(Unpooled.copiedBuffer("111end222end333end".getBytes()));
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("服务端发过来的内容:" + msg);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
//异常时关闭
ctx.close();
}
}