服务端
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
// 自定义网络服务器
public class MySimpleServer {
public static void main(String[] args) throws InterruptedException {
ServerBootstrap sb = new ServerBootstrap();
NioEventLoopGroup group = new NioEventLoopGroup();
try {
sb.group(group);
sb.channel(NioServerSocketChannel.class);
sb.localAddress(8090);
sb.childHandler(new MySimpleChannelInitializer());
ChannelFuture future = sb.bind();
future.sync().channel().closeFuture().sync();
} finally {
group.shutdownGracefully().sync();
}
}
}
// 通道初始化
class MySimpleChannelInitializer extends ChannelInitializer<SocketChannel> {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
// 增加字符串编解码器,否则自定义消息处理器无法解析字节数据
ch.pipeline().addLast(new StringDecoder());
ch.pipeline().addLast(new StringEncoder());
// 将自定义服务器消息处理器加入通道
ch.pipeline().addLast(new MySimpleHandler());
}
}
// 消息处理器
class MySimpleHandler extends SimpleChannelInboundHandler<String> {
// 读取消息
@Override
protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
System.out.println("Server Receive:" + msg);
ctx.writeAndFlush("this is server");
}
}
客户端
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.SimpleChannelInboundHandler;
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;
import io.netty.handler.codec.string.StringEncoder;
import java.net.InetSocketAddress;
public class MySimpleClient {
public static void main(String[] args) throws InterruptedException {
Bootstrap bs = new Bootstrap();
NioEventLoopGroup group = new NioEventLoopGroup();
try {
bs.group(group);
bs.channel(NioSocketChannel.class);
bs.handler(new MyClientChannelInitializer());
ChannelFuture future = bs.connect(new InetSocketAddress("localhost", 8090));
future.sync().channel().closeFuture().sync();
} finally {
group.shutdownGracefully().sync();
}
}
}
// 客户端通道初始化
class MyClientChannelInitializer extends ChannelInitializer<SocketChannel> {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
// 增加字符串编解码器,否则自定义消息处理器无法解析字节数据
ch.pipeline().addLast(new StringDecoder());
ch.pipeline().addLast(new StringEncoder());
// 将客户端消息处理器加入通道中
ch.pipeline().addLast(new MyClientHandler());
}
}
// 自定义客户端消息处理器
class MyClientHandler extends SimpleChannelInboundHandler<String> {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
ctx.writeAndFlush("hello ,this is client");
}
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, String string) throws Exception {
//
System.out.println("Client Receive:" + string);
}
}