首先是服务器启动类,老生常谈了
/**
* @author sss
* 服务器启动类
*/
public class EchoServer {
private final int port;
private Logger log = LoggerFactory.getLogger(this.getClass());
public EchoServer(int port)
{
this.port=port;
}
//启动服务器
public void start() throws Exception{
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try
{
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.localAddress(new InetSocketAddress(port))
.childOption(ChannelOption.SO_KEEPALIVE, true) //2小时没有信息流则开启
.option(ChannelOption.SO_BACKLOG, 1024)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
//ch.pipeline().addLast(new ReadTimeoutHandler(5)); //5秒没有读事件就关掉
//ch.pipeline().addLast(new StringDecoder(CharsetUtil.UTF_8));
ch.pipeline().addLast(new MyDecoder()); //自定义的解码
ch.pipeline().addLast(new StringEncoder(CharsetUtil.UTF_8));
ch.pipeline().addLast(new TcpHandler());
}
});
ChannelFuture f=b.bind().sync();
f.channel().closeFuture().sync();
}
finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
}
2、 重写decoder
public class MyDecoder extends ByteToMessageDecoder {
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List
使用
@ChannelHandler.Sharable
public class TcpHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception{
Channel incoming = ctx.channel();
String body = (String)msg;
System.out.println("原始报文-------" +
incoming.remoteAddress().toString()+ "----" + body);
long dec_num = Long.parseLong(body, 16);
MyTools myTools=new MyTools();
myTools.writeToClient("1002050a840336010102328002021003",ctx,"测试");
}
}
数据处理
public class MyTools {
//十六进制字符转十进制
public int covert(String content){
int number=0;
String [] HighLetter = {"A","B","C","D","E","F"};
Map map = new HashMap<>();
for(int i = 0;i <= 9;i++){
map.put(i+"",i);
}
for(int j= 10;j
帮初学者添上Application
@SpringBootApplication
public class DemoApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void run(String... strings) throws Exception {
EchoServer echoServer = new EchoServer(7000);
echoServer.start();
}
}
很简单的。祝大家学习愉快。