Demo netty
This commit is contained in:
parent
3835e68e21
commit
100656c611
@ -4,54 +4,31 @@ import java.nio.ByteBuffer;
|
|||||||
|
|
||||||
public class HexToCharArray {
|
public class HexToCharArray {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
// // 示例16进制字符串的ByteBuffer
|
/***示例16进制数组转字符数组****/
|
||||||
// ByteBuffer buffer = ByteBuffer.wrap(new byte[]{0x4A, 0x6B, 0x4C, 0x6D});
|
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) {
|
|
||||||
// 确保读取时从开头开始
|
|
||||||
buffer.rewind();
|
buffer.rewind();
|
||||||
|
|
||||||
// 创建字符数组,每个字节转换为一个字符
|
// 创建字符数组,每个字节转换为一个字符
|
||||||
char[] charArray = new char[buffer.remaining()];
|
char[] charArray = new char[buffer.remaining()];
|
||||||
|
|
||||||
for (int i = 0; i < charArray.length; i++) {
|
for (int i = 0; i < charArray.length; i++) {
|
||||||
// 逐字节读取并转换为字符
|
// 逐字节读取并转换为字符
|
||||||
charArray[i] = (char) buffer.get();
|
charArray[i] = (char) buffer.get();
|
||||||
}
|
}
|
||||||
|
// 打印字符数组
|
||||||
|
System.out.println(charArray);
|
||||||
|
|
||||||
return charArray;
|
/***示例16进制字符串转字符数组***/
|
||||||
}
|
String hexString = "4A6B4C6D";
|
||||||
public static char[] hexStringToCharArray(String hexString) {
|
|
||||||
// 每两个十六进制字符表示一个字节,所以字符数组的长度为十六进制字符串长度的一半
|
|
||||||
int length = hexString.length() / 2;
|
int length = hexString.length() / 2;
|
||||||
char[] charArray = new char[length];
|
char[] charArray2 = new char[length];
|
||||||
|
|
||||||
for (int i = 0; i < length; i++) {
|
for (int i = 0; i < length; i++) {
|
||||||
// 取出两个十六进制字符并解析为一个字节
|
// 取出两个十六进制字符并解析为一个字节
|
||||||
String hexPair = hexString.substring(2 * i, 2 * i + 2);
|
String hexPair = hexString.substring(2 * i, 2 * i + 2);
|
||||||
int byteValue = Integer.parseInt(hexPair, 16);
|
int byteValue = Integer.parseInt(hexPair, 16);
|
||||||
|
|
||||||
// 转换为字符并存入数组
|
// 转换为字符并存入数组
|
||||||
charArray[i] = (char) byteValue;
|
charArray2[i] = (char) byteValue;
|
||||||
}
|
}
|
||||||
|
// 打印字符数组
|
||||||
return charArray;
|
System.out.println(charArray2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,10 +1,7 @@
|
|||||||
package com.example.demo.Service;
|
package com.example.demo.Service;
|
||||||
|
|
||||||
import io.netty.bootstrap.Bootstrap;
|
import io.netty.bootstrap.Bootstrap;
|
||||||
import io.netty.channel.Channel;
|
import io.netty.channel.*;
|
||||||
import io.netty.channel.ChannelInitializer;
|
|
||||||
import io.netty.channel.ChannelOption;
|
|
||||||
import io.netty.channel.EventLoopGroup;
|
|
||||||
import io.netty.channel.nio.NioEventLoopGroup;
|
import io.netty.channel.nio.NioEventLoopGroup;
|
||||||
import io.netty.channel.socket.SocketChannel;
|
import io.netty.channel.socket.SocketChannel;
|
||||||
import io.netty.channel.socket.nio.NioSocketChannel;
|
import io.netty.channel.socket.nio.NioSocketChannel;
|
||||||
@ -13,7 +10,7 @@ import org.springframework.stereotype.Component;
|
|||||||
@Component
|
@Component
|
||||||
public class nettyClient {
|
public class nettyClient {
|
||||||
|
|
||||||
private final String host = "localhost"; // 目标服务器地址
|
private final String host = "127.0.0.1"; // 目标服务器地址
|
||||||
private final int port = 8081; // 目标服务器端口
|
private final int port = 8081; // 目标服务器端口
|
||||||
|
|
||||||
public void start() throws InterruptedException {
|
public void start() throws InterruptedException {
|
||||||
@ -25,7 +22,7 @@ public class nettyClient {
|
|||||||
Bootstrap bootstrap = new Bootstrap();
|
Bootstrap bootstrap = new Bootstrap();
|
||||||
bootstrap.group(group) // 指定线程组
|
bootstrap.group(group) // 指定线程组
|
||||||
.channel(NioSocketChannel.class) // 指定使用 NIO 传输的Channel
|
.channel(NioSocketChannel.class) // 指定使用 NIO 传输的Channel
|
||||||
.option(ChannelOption.SO_KEEPALIVE, true) // 保持长连接
|
//.option(ChannelOption.SO_KEEPALIVE, true) // 保持长连接
|
||||||
.handler(new ChannelInitializer<SocketChannel>() {
|
.handler(new ChannelInitializer<SocketChannel>() {
|
||||||
@Override
|
@Override
|
||||||
protected void initChannel(SocketChannel ch) {
|
protected void initChannel(SocketChannel ch) {
|
||||||
@ -35,13 +32,14 @@ public class nettyClient {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// 4. 连接服务器
|
// 4. 连接服务器
|
||||||
Channel channel = bootstrap.connect(host, port).sync().channel();
|
ChannelFuture future = bootstrap.connect(host, port).sync();
|
||||||
|
|
||||||
// 5. 向服务器发送消息
|
// 5. 向服务器发送消息
|
||||||
channel.writeAndFlush("Hello, Netty Server!");
|
//future.channel().writeAndFlush("Hello, Netty Server!");
|
||||||
|
|
||||||
// 6. 等待连接关闭
|
// 6. 等待连接关闭
|
||||||
channel.closeFuture().sync();
|
future.channel().closeFuture().sync();
|
||||||
|
System.out.println("aaa");
|
||||||
} finally {
|
} finally {
|
||||||
// 7. 关闭线程组
|
// 7. 关闭线程组
|
||||||
group.shutdownGracefully();
|
group.shutdownGracefully();
|
||||||
|
|||||||
@ -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.ChannelHandlerContext;
|
||||||
import io.netty.channel.ChannelInboundHandlerAdapter;
|
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
|
@Override
|
||||||
public void channelRead(ChannelHandlerContext ctx, Object msg) {
|
public void channelRead(ChannelHandlerContext ctx, Object msg) {
|
||||||
// 处理服务器的响应
|
ByteBuf byteBuf = (ByteBuf)msg;
|
||||||
System.out.println("Received message from server: " + 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
|
@Override
|
||||||
|
|||||||
@ -2,16 +2,15 @@ package com.example.demo.Service;
|
|||||||
|
|
||||||
|
|
||||||
import io.netty.bootstrap.ServerBootstrap;
|
import io.netty.bootstrap.ServerBootstrap;
|
||||||
import io.netty.channel.ChannelFuture;
|
import io.netty.channel.*;
|
||||||
import io.netty.channel.ChannelInitializer;
|
|
||||||
import io.netty.channel.ChannelPipeline;
|
|
||||||
import io.netty.channel.EventLoopGroup;
|
|
||||||
import io.netty.channel.nio.NioEventLoopGroup;
|
import io.netty.channel.nio.NioEventLoopGroup;
|
||||||
import io.netty.channel.socket.SocketChannel;
|
import io.netty.channel.socket.SocketChannel;
|
||||||
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
||||||
import io.netty.handler.codec.string.StringDecoder;
|
import io.netty.handler.codec.string.StringDecoder;
|
||||||
import io.netty.handler.codec.string.StringEncoder;
|
import io.netty.handler.codec.string.StringEncoder;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
public class nettyServer {
|
public class nettyServer {
|
||||||
private final int port;
|
private final int port;
|
||||||
|
|
||||||
@ -20,8 +19,8 @@ public class nettyServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void start() throws InterruptedException {
|
public void start() throws InterruptedException {
|
||||||
EventLoopGroup bossGroup = new NioEventLoopGroup();
|
EventLoopGroup bossGroup = new NioEventLoopGroup(1);//线程
|
||||||
EventLoopGroup workerGroup = new NioEventLoopGroup();
|
EventLoopGroup workerGroup = new NioEventLoopGroup();//8
|
||||||
try {
|
try {
|
||||||
ServerBootstrap b = new ServerBootstrap();
|
ServerBootstrap b = new ServerBootstrap();
|
||||||
b.group(bossGroup, workerGroup)
|
b.group(bossGroup, workerGroup)
|
||||||
@ -30,15 +29,15 @@ public class nettyServer {
|
|||||||
@Override
|
@Override
|
||||||
public void initChannel(SocketChannel ch) {
|
public void initChannel(SocketChannel ch) {
|
||||||
ChannelPipeline p = ch.pipeline();
|
ChannelPipeline p = ch.pipeline();
|
||||||
p.addLast(new StringDecoder());
|
|
||||||
p.addLast(new StringEncoder());
|
|
||||||
p.addLast(new nettyServerHandler());
|
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();
|
f.channel().closeFuture().sync();
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
bossGroup.shutdownGracefully();
|
bossGroup.shutdownGracefully();
|
||||||
workerGroup.shutdownGracefully();
|
workerGroup.shutdownGracefully();
|
||||||
|
|||||||
@ -1,13 +1,20 @@
|
|||||||
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.ChannelHandlerContext;
|
||||||
import io.netty.channel.ChannelInboundHandlerAdapter;
|
import io.netty.channel.ChannelInboundHandlerAdapter;
|
||||||
|
import io.netty.util.CharsetUtil;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
public class nettyServerHandler extends ChannelInboundHandlerAdapter {
|
public class nettyServerHandler extends ChannelInboundHandlerAdapter {
|
||||||
@Override
|
@Override
|
||||||
public void channelRead(ChannelHandlerContext ctx, Object msg) {
|
public void channelRead(ChannelHandlerContext ctx, Object msg) {
|
||||||
System.out.println("Received message: " + msg);
|
ByteBuf byteBuf = (ByteBuf)msg;
|
||||||
ctx.writeAndFlush("Hello from Netty Server!\n");
|
System.out.println("Received message: " + byteBuf.toString(CharsetUtil.UTF_8));
|
||||||
|
ctx.channel().writeAndFlush(Unpooled.copiedBuffer("Hello from Netty Server",CharsetUtil.UTF_8));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user