|
@@ -0,0 +1,113 @@
|
|
|
+package com.huimv.eartag.server;
|
|
|
+
|
|
|
+import cn.hutool.core.util.CharsetUtil;
|
|
|
+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.handler.logging.LogLevel;
|
|
|
+import io.netty.handler.logging.LoggingHandler;
|
|
|
+//import io.netty.util.CharsetUtil;
|
|
|
+import cn.hutool.core.util.CharsetUtil;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Project : huimv.shiwan
|
|
|
+ * @Package : com.huimv.biosafety.uface.controller
|
|
|
+ * @Description : TODO
|
|
|
+ * @Version : 1.0
|
|
|
+ * @Author : ZhuoNing
|
|
|
+ * @Create : 2020-12-25
|
|
|
+ **/
|
|
|
+@Component
|
|
|
+public class EartagServer {
|
|
|
+ @Autowired
|
|
|
+ private EartagServerHandler2 serverHandler;
|
|
|
+ //监听端口
|
|
|
+ private int port = 8012;
|
|
|
+ private int port2 = 8013;
|
|
|
+ //创建构造方法
|
|
|
+ public EartagServer(){
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) throws InterruptedException {
|
|
|
+ new EartagServer().run();
|
|
|
+ }
|
|
|
+/**
|
|
|
+ *
|
|
|
+ * 功能描述: 启动方法前台多个服务 处理多个线程
|
|
|
+ *
|
|
|
+ * @param:
|
|
|
+ * @return:
|
|
|
+ * @auther: LiGang
|
|
|
+ * @date: 2019/3/26 11:31
|
|
|
+ */
|
|
|
+ /**
|
|
|
+ * 启动流程
|
|
|
+ */
|
|
|
+ public void run() throws InterruptedException {
|
|
|
+ //配置服务端线程组
|
|
|
+ EventLoopGroup bossGroup=new NioEventLoopGroup();
|
|
|
+ EventLoopGroup workGroup=new NioEventLoopGroup();
|
|
|
+
|
|
|
+ try{
|
|
|
+ //引导整个server的启动
|
|
|
+ ServerBootstrap serverBootstrap = new ServerBootstrap();
|
|
|
+ serverBootstrap.group(bossGroup,workGroup)
|
|
|
+ .channel(NioServerSocketChannel.class) //指定处理的连接类型
|
|
|
+ .option(ChannelOption.SO_REUSEADDR, true)
|
|
|
+ .childHandler(new ChannelInitializer<SocketChannel>() {
|
|
|
+ @Override
|
|
|
+ protected void initChannel(SocketChannel socketChannel) throws Exception {
|
|
|
+ socketChannel.pipeline().addLast(serverHandler);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ System.out.println("# 耳标及采集器设备数据接收服务器已经启动。#");
|
|
|
+ System.out.println("# 准备接收数据:");
|
|
|
+ //绑定端口,同步等待成功
|
|
|
+ ChannelFuture cf = serverBootstrap.bind(port).sync();
|
|
|
+ ChannelFuture cf2 = serverBootstrap.bind(port2).sync();
|
|
|
+ // 等待服务端监听端口关闭
|
|
|
+ cf.channel().closeFuture().sync();
|
|
|
+ cf2.channel().closeFuture().sync();
|
|
|
+ }finally {
|
|
|
+ //优雅的退出
|
|
|
+ bossGroup.shutdownGracefully();
|
|
|
+ workGroup.shutdownGracefully();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void run2() throws InterruptedException {
|
|
|
+ ServerBootstrap b = new ServerBootstrap();
|
|
|
+ EventLoopGroup bossGroup = new NioEventLoopGroup();
|
|
|
+ EventLoopGroup workerGroup = new NioEventLoopGroup();
|
|
|
+
|
|
|
+ b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
|
|
|
+ .option(ChannelOption.SO_REUSEADDR, true)
|
|
|
+ .childHandler(new ChannelInitializer() {
|
|
|
+ @Override
|
|
|
+ protected void initChannel(Channel ch) throws Exception {
|
|
|
+ ch.pipeline()
|
|
|
+ .addLast("logging", new LoggingHandler(LogLevel.INFO))
|
|
|
+ .addLast(new SimpleChannelInboundHandler() {
|
|
|
+ @Override
|
|
|
+ protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
|
|
|
+ ByteBuf data = (ByteBuf) msg;
|
|
|
+ String clientAskText = data.toString(io.netty.util.CharsetUtil.UTF_8);
|
|
|
+// String strMsg = msg.toString(CharsetUtil.UTF_8);
|
|
|
+ System.out.println("clientAskText>>"+clientAskText);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+// b.bind(port).sync();
|
|
|
+// System.out.println("tcp server("+port+") is started..");
|
|
|
+
|
|
|
+ b.bind(port2).sync();
|
|
|
+ System.out.println("tcp server("+port2+") is started..");
|
|
|
+ }
|
|
|
+}
|