diff --git a/src/main/java/com/example/demo/Service/HexToCharArray.java b/src/main/java/com/example/demo/Service/HexToCharArray.java index a51f1f9..fed7ed3 100644 --- a/src/main/java/com/example/demo/Service/HexToCharArray.java +++ b/src/main/java/com/example/demo/Service/HexToCharArray.java @@ -4,54 +4,31 @@ import java.nio.ByteBuffer; public class HexToCharArray { public static void main(String[] args) { -// // 示例16进制字符串的ByteBuffer -// ByteBuffer buffer = ByteBuffer.wrap(new byte[]{0x4A, 0x6B, 0x4C, 0x6D}); -// -// // 将 ByteBuffer 转换为字符数组 -// char[] charArray = byteBufferToCharArray(buffer); -// -// // 打印字符数组 -// System.out.println(charArray); - - String hexString = "4A6B4C6D"; - - // 将16进制字符串转换为字符数组 - char[] charArray = hexStringToCharArray(hexString); - - // 打印字符数组 - System.out.println(charArray); - } - - public static char[] byteBufferToCharArray(ByteBuffer buffer) { - // 确保读取时从开头开始 + /***示例16进制数组转字符数组****/ + ByteBuffer buffer = ByteBuffer.wrap(new byte[]{0x4A, 0x6B, 0x4C, 0x6D}); buffer.rewind(); - // 创建字符数组,每个字节转换为一个字符 char[] charArray = new char[buffer.remaining()]; - for (int i = 0; i < charArray.length; i++) { // 逐字节读取并转换为字符 charArray[i] = (char) buffer.get(); } + // 打印字符数组 + System.out.println(charArray); - return charArray; - } - public static char[] hexStringToCharArray(String hexString) { - // 每两个十六进制字符表示一个字节,所以字符数组的长度为十六进制字符串长度的一半 + /***示例16进制字符串转字符数组***/ + String hexString = "4A6B4C6D"; int length = hexString.length() / 2; - char[] charArray = new char[length]; + char[] charArray2 = new char[length]; for (int i = 0; i < length; i++) { // 取出两个十六进制字符并解析为一个字节 String hexPair = hexString.substring(2 * i, 2 * i + 2); int byteValue = Integer.parseInt(hexPair, 16); - // 转换为字符并存入数组 - charArray[i] = (char) byteValue; + charArray2[i] = (char) byteValue; } - - return charArray; + // 打印字符数组 + System.out.println(charArray2); } - - } diff --git a/src/main/java/com/example/demo/Service/nettyClient.java b/src/main/java/com/example/demo/Service/nettyClient.java index cb88d7b..92aa67c 100644 --- a/src/main/java/com/example/demo/Service/nettyClient.java +++ b/src/main/java/com/example/demo/Service/nettyClient.java @@ -1,10 +1,7 @@ package com.example.demo.Service; import io.netty.bootstrap.Bootstrap; -import io.netty.channel.Channel; -import io.netty.channel.ChannelInitializer; -import io.netty.channel.ChannelOption; -import io.netty.channel.EventLoopGroup; +import io.netty.channel.*; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; @@ -13,7 +10,7 @@ import org.springframework.stereotype.Component; @Component public class nettyClient { - private final String host = "localhost"; // 目标服务器地址 + private final String host = "127.0.0.1"; // 目标服务器地址 private final int port = 8081; // 目标服务器端口 public void start() throws InterruptedException { @@ -25,7 +22,7 @@ public class nettyClient { Bootstrap bootstrap = new Bootstrap(); bootstrap.group(group) // 指定线程组 .channel(NioSocketChannel.class) // 指定使用 NIO 传输的Channel - .option(ChannelOption.SO_KEEPALIVE, true) // 保持长连接 + //.option(ChannelOption.SO_KEEPALIVE, true) // 保持长连接 .handler(new ChannelInitializer() { @Override protected void initChannel(SocketChannel ch) { @@ -35,13 +32,14 @@ public class nettyClient { }); // 4. 连接服务器 - Channel channel = bootstrap.connect(host, port).sync().channel(); + ChannelFuture future = bootstrap.connect(host, port).sync(); // 5. 向服务器发送消息 - channel.writeAndFlush("Hello, Netty Server!"); + //future.channel().writeAndFlush("Hello, Netty Server!"); // 6. 等待连接关闭 - channel.closeFuture().sync(); + future.channel().closeFuture().sync(); + System.out.println("aaa"); } finally { // 7. 关闭线程组 group.shutdownGracefully(); diff --git a/src/main/java/com/example/demo/Service/nettyClientHandler.java b/src/main/java/com/example/demo/Service/nettyClientHandler.java index a809f5e..afe259d 100644 --- a/src/main/java/com/example/demo/Service/nettyClientHandler.java +++ b/src/main/java/com/example/demo/Service/nettyClientHandler.java @@ -1,17 +1,33 @@ -package com.example.demo.Service; + package com.example.demo.Service; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import io.netty.buffer.UnpooledByteBufAllocator; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; +import io.netty.util.CharsetUtil; -public class nettyClientHandler extends ChannelInboundHandlerAdapter { +import java.nio.charset.StandardCharsets; + + public class nettyClientHandler extends ChannelInboundHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) { - // 处理服务器的响应 - System.out.println("Received message from server: " + msg); + ByteBuf byteBuf = (ByteBuf)msg; + System.out.println("Received message from server: " + byteBuf.toString(StandardCharsets.UTF_8)); + } + + @Override + //当 Channel 已经连接/绑定并且已经就绪时调用 + public void channelActive(ChannelHandlerContext ctx) throws Exception { + + Object a = Unpooled.copiedBuffer("Hello from Netty Server",CharsetUtil.UTF_8); + + ctx.channel().writeAndFlush(a); + System.out.println("send message to server: " + "Hello, Netty Server!"); } @Override diff --git a/src/main/java/com/example/demo/Service/nettyServer.java b/src/main/java/com/example/demo/Service/nettyServer.java index 982806a..206fa52 100644 --- a/src/main/java/com/example/demo/Service/nettyServer.java +++ b/src/main/java/com/example/demo/Service/nettyServer.java @@ -2,16 +2,15 @@ package com.example.demo.Service; import io.netty.bootstrap.ServerBootstrap; -import io.netty.channel.ChannelFuture; -import io.netty.channel.ChannelInitializer; -import io.netty.channel.ChannelPipeline; -import io.netty.channel.EventLoopGroup; +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.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringEncoder; +import lombok.extern.slf4j.Slf4j; +@Slf4j public class nettyServer { private final int port; @@ -20,8 +19,8 @@ public class nettyServer { } public void start() throws InterruptedException { - EventLoopGroup bossGroup = new NioEventLoopGroup(); - EventLoopGroup workerGroup = new NioEventLoopGroup(); + EventLoopGroup bossGroup = new NioEventLoopGroup(1);//线程 + EventLoopGroup workerGroup = new NioEventLoopGroup();//8 try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup) @@ -30,15 +29,15 @@ public class nettyServer { @Override public void initChannel(SocketChannel ch) { ChannelPipeline p = ch.pipeline(); - p.addLast(new StringDecoder()); - p.addLast(new StringEncoder()); p.addLast(new nettyServerHandler()); } }); - ChannelFuture f = b.bind(port).sync(); - System.out.println("Netty server started on port " + port); + + ChannelFuture f = b.bind(port).sync();//成功之后在执行 + f.addListener((ChannelFutureListener)future -> log.info("监听端口:{}",future.isSuccess())); f.channel().closeFuture().sync(); + } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); diff --git a/src/main/java/com/example/demo/Service/nettyServerHandler.java b/src/main/java/com/example/demo/Service/nettyServerHandler.java index 6d93bc5..69ade7d 100644 --- a/src/main/java/com/example/demo/Service/nettyServerHandler.java +++ b/src/main/java/com/example/demo/Service/nettyServerHandler.java @@ -1,13 +1,20 @@ package com.example.demo.Service; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import io.netty.buffer.UnpooledByteBufAllocator; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; +import io.netty.util.CharsetUtil; + +import java.nio.charset.StandardCharsets; public class nettyServerHandler extends ChannelInboundHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) { - System.out.println("Received message: " + msg); - ctx.writeAndFlush("Hello from Netty Server!\n"); + ByteBuf byteBuf = (ByteBuf)msg; + System.out.println("Received message: " + byteBuf.toString(CharsetUtil.UTF_8)); + ctx.channel().writeAndFlush(Unpooled.copiedBuffer("Hello from Netty Server",CharsetUtil.UTF_8)); } @Override