服务端
package echo;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.InetSocketAddress;
/**
* @author xuanxuan
* @date 2024/1/16 14:09
* 说明:基于netty的服务器
*/
public class EchoServer {
private static final Logger LOG = LoggerFactory.getLogger(EchoServer.class);
private final int port;
public EchoServer(int port) {
this.port = port;
}
public static void main(String[] args) throws InterruptedException {
int port = 9999;
EchoServer echoServer = new EchoServer(port);
LOG.info("服务器即将启动");
echoServer.start();
LOG.info("服务器关闭");
}
private void start() throws InterruptedException {
//线程组
NioEventLoopGroup group = new NioEventLoopGroup();
try{
//服务端启动
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(group)
.channel(NioServerSocketChannel.class)//接收连接
.localAddress(new InetSocketAddress(port))
// .handler() 一般不指定
.childHandler(
new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception { //SocketChannel来处理业务 实际网络通信
ch.pipeline()
.addLast(new EchoServerHandler());
}
}
);
ChannelFuture future = bootstrap.bind().sync();//异步操作
future.channel().closeFuture().sync();
}finally {
group.shutdownGracefully().sync();
}
}
}
服务端handler
package echo;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;
import java.nio.charset.Charset;
/**
* @author xuanxuan
* @date 2024/1/16 14:23
*/
public class EchoServerHandler extends ChannelInboundHandlerAdapter {
//服务端接收后不做处理 直接写入
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
//收到的msg不更改发送给对端
ByteBuf in = (ByteBuf) msg;//缓冲区
System.out.println("server accept:"+in.toString(CharsetUtil.UTF_8));
ctx.writeAndFlush(in);//写入缓冲区
ctx.close();
}
//active 后也可以做业务处理
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}
客户端
package echo;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;
/**
* @author xuanxuan
* @date 2024/1/16 14:45
*/
public class EchoClientHandler extends SimpleChannelInboundHandler<ByteBuf> {
@Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf byteBuf) throws Exception {
System.out.println("client accept"+byteBuf.toString(CharsetUtil.UTF_8 ));
ctx.close();
}
//相当于连接成功 处理业务
//向服务端发送 hello,xuanxuan
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
ctx.writeAndFlush(Unpooled.copiedBuffer(" hello,xuanxuan",CharsetUtil.UTF_8));
}
}
package echo;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.InetSocketAddress;
/**
* @author xuanxuan
* @date 2024/1/16 14:40
*/
public class EchoClient {
private static final Logger LOG = LoggerFactory.getLogger(EchoServer.class);
private final String host;
private final int port;
public EchoClient(int port,String host) {
this.host = host;
this.port = port;
}
public static void main(String[] args) throws InterruptedException {
new EchoClient(9999,"127.0.0.1").start();
}
private void start() throws InterruptedException {
//线程组
NioEventLoopGroup group = new NioEventLoopGroup();
try{
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(group)
.channel(NioSocketChannel.class)
.remoteAddress(new InetSocketAddress(host,port))
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new EchoClientHandler());
}
});
ChannelFuture future = bootstrap.connect().sync();
future.channel().closeFuture().sync();
}finally {
group.shutdownGracefully().sync();
}
}
}