网络编程基础-netty

master
bingor 3 years ago
parent ab1e5374d7
commit 1b6a72eddc

@ -0,0 +1,80 @@
package com.msb.netty;/**
* @Author bingor
* @Date 2022/10/27 17:33
* @Description: com.msb.netty
* @Version: 1.0
*/
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.util.ReferenceCountUtil;
import io.netty.util.ReferenceCounted;
/**
*@ClassName Client
*@Description TODO
*@Author bingor
*@Date 2022/10/27 17:33
*@Version 3.0
*/
public class Client {
public void clientStart() {
EventLoopGroup workerGroup = new NioEventLoopGroup();
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(workerGroup)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
System.out.println("client initialized");
socketChannel.pipeline().addLast(new ClientHandler());
}
});
System.out.println("start to connect....");
try {
ChannelFuture future = bootstrap.connect("127.0.0.1", 8888).sync();
future.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
workerGroup.shutdownGracefully();
}
}
public static void main(String[] args) {
new Client().clientStart();
}
}
class ClientHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
// super.channelActive(ctx);
System.out.println("channel is activated.");
final ChannelFuture future = ctx.writeAndFlush(Unpooled.copiedBuffer("hello netty server".getBytes()));
future.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture channelFuture) throws Exception {
System.out.println("msg send");
}
});
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
// super.channelRead(ctx, msg);
try {
ByteBuf buf = (ByteBuf) msg;
System.out.println(buf.toString());
} finally {
ReferenceCountUtil.release(msg);
}
}
}

@ -0,0 +1,82 @@
package com.msb.netty;/**
* @Author bingor
* @Date 2022/10/27 16:47
* @Description: com.msb.netty
* @Version: 1.0
*/
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.util.CharsetUtil;
import java.nio.charset.Charset;
/**
*@ClassName Server
*@Description TODO
*@Author bingor
*@Date 2022/10/27 16:47
*@Version 3.0
*/
public class Server {
private int port;
public Server(int port) {
this.port = port;
}
public void serverStart() {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(new Handler());
}
});
try {
ChannelFuture future = bootstrap.bind(this.port).sync();
future.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
public static void main(String[] args) {
new Server(8888).serverStart();
}
}
class Handler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
// super.channelRead(ctx, msg);
System.out.println("server: channel read");
ByteBuf buf = (ByteBuf) msg;
System.out.println(new String(buf.toString(CharsetUtil.UTF_8)));
ctx.writeAndFlush(msg);
ctx.close();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
// super.exceptionCaught(ctx, cause);
cause.printStackTrace();
ctx.close();
}
}
Loading…
Cancel
Save