netty发送定长字符串 netty
程序员文章站
2022-05-06 20:09:09
...
使用telnet连接netty:发送定长字符串
//1、启动Server,输入telnet命令:telnet localhost 8088
public class NettyServer {
private int port=8088;
public static void main(String[] args) {
NettyServer server = new NettyServer();
server.run();
}
void run() {
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
public void initChannel(SocketChannel ch) {
ChannelPipeline p = ch.pipeline();
//客户端发送给服务端:hello server,im a client这符串,
//该字符串占用24字节,所以在服务端channelpipeline里面添加一个长度
//为24的定长解码器和二进制转换为string的解码器
p.addLast(new FixedLengthFrameDecoder(24));
p.addLast(new StringDecoder()); //与SimpleChannelInboundHandler<String> 泛型对应
p.addLast(new ServerHandler2());
}}
).option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
ChannelFuture f = b.bind(port).sync();
f.channel().closeFuture().sync();
}catch(Exception e) {
e.printStackTrace();
}finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
public class ServerHandler2 extends SimpleChannelInboundHandler<String> {
protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
System.out.println("from client: "+msg + msg.getClass());
String sendContent="hello client,im server";
ByteBuf sendMsg = Unpooled.buffer(sendContent.length());
sendMsg.writeBytes(sendContent.getBytes());
ctx.writeAndFlush(Unpooled.copiedBuffer(sendMsg));
}
}
客户端:
public class NettyClient {
private String host="localhost";
private int port = 8088;
public static void main(String[] args) {
new NettyClient().run();
}
public void run() {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group).channel(NioSocketChannel.class)
.option(ChannelOption.TCP_NODELAY, true)
//.remoteAddress(new InetSocketAddress(host, port))
.handler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel ch)throws Exception {
ChannelPipeline p=ch.pipeline();
//服务端发送:hello client,im server字符串,占用22字节
p.addLast(new FixedLengthFrameDecoder(22)); //缓冲区达到22字节,才写入通道
p.addLast(new StringDecoder());
p.addLast("handler",new ClientHandler2());
}
});
ChannelFuture f = b.connect(host,port).sync();
f.channel().closeFuture().sync();
}catch(Exception e) {
e.printStackTrace();
}
finally {
try {
group.shutdownGracefully().sync();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class ClientHandler2 extends SimpleChannelInboundHandler<String>{
protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
System.out.println("client channelRead0.."+msg);
}
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("client channelActive..");
ctx.writeAndFlush(Unpooled.copiedBuffer("hello server,im a client".getBytes()));
}
}
//1、启动Server,输入telnet命令:telnet localhost 8088
public class NettyServer {
private int port=8088;
public static void main(String[] args) {
NettyServer server = new NettyServer();
server.run();
}
void run() {
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
public void initChannel(SocketChannel ch) {
ChannelPipeline p = ch.pipeline();
//客户端发送给服务端:hello server,im a client这符串,
//该字符串占用24字节,所以在服务端channelpipeline里面添加一个长度
//为24的定长解码器和二进制转换为string的解码器
p.addLast(new FixedLengthFrameDecoder(24));
p.addLast(new StringDecoder()); //与SimpleChannelInboundHandler<String> 泛型对应
p.addLast(new ServerHandler2());
}}
).option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
ChannelFuture f = b.bind(port).sync();
f.channel().closeFuture().sync();
}catch(Exception e) {
e.printStackTrace();
}finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
public class ServerHandler2 extends SimpleChannelInboundHandler<String> {
protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
System.out.println("from client: "+msg + msg.getClass());
String sendContent="hello client,im server";
ByteBuf sendMsg = Unpooled.buffer(sendContent.length());
sendMsg.writeBytes(sendContent.getBytes());
ctx.writeAndFlush(Unpooled.copiedBuffer(sendMsg));
}
}
客户端:
public class NettyClient {
private String host="localhost";
private int port = 8088;
public static void main(String[] args) {
new NettyClient().run();
}
public void run() {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group).channel(NioSocketChannel.class)
.option(ChannelOption.TCP_NODELAY, true)
//.remoteAddress(new InetSocketAddress(host, port))
.handler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel ch)throws Exception {
ChannelPipeline p=ch.pipeline();
//服务端发送:hello client,im server字符串,占用22字节
p.addLast(new FixedLengthFrameDecoder(22)); //缓冲区达到22字节,才写入通道
p.addLast(new StringDecoder());
p.addLast("handler",new ClientHandler2());
}
});
ChannelFuture f = b.connect(host,port).sync();
f.channel().closeFuture().sync();
}catch(Exception e) {
e.printStackTrace();
}
finally {
try {
group.shutdownGracefully().sync();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class ClientHandler2 extends SimpleChannelInboundHandler<String>{
protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
System.out.println("client channelRead0.."+msg);
}
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("client channelActive..");
ctx.writeAndFlush(Unpooled.copiedBuffer("hello server,im a client".getBytes()));
}
}