package com.huimv.acquisition.socket.eartagrecive.eartagsocket;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.*;
import java.net.Socket;

/**
 * @Project : huimv.shiwan
 * @Package : com.huimv.biosafety.uface.controller
 * @Description : TODO
 * @Version : 1.0
 * @Author : ZhuoNing
 * @Create : 2020-12-25
 **/
@Component
public class EartagServer2 {
    @Autowired
    private EartagServerHandler2 serverHandler;
    //外面的版本    金华的端口     10024    10025  部署在31 服务器上面好了    10023是test1平台的
    //创建构造方法

    private int port = 10023;
    public EartagServer2(){
    }

   
/**
 *
 * 功能描述: 启动方法前台多个服务  处理多个线程
 *
 * @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)    //指定处理的连接类型
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
//                            socketChannel.pipeline().addLast(new EartagServerHandler2());
                            socketChannel.pipeline().addLast(serverHandler);
                        }
                    });
            System.out.println("Echo 服务器启动ing--监听端口"+port);
            //绑定端口，同步等待成功
            ChannelFuture cf = serverBootstrap.bind(port).sync();
            System.out.println("ok");
            // 等待服务端监听端口关闭
            cf.channel().closeFuture().sync();

        }finally {
            //优雅的退出
            bossGroup.shutdownGracefully();
            workGroup.shutdownGracefully();
        }
    }



    public static void main(String[] args) throws IOException {
        //建立socket向服务器发送连接请求
        Socket socket = new Socket("127.0.0.1", 10023);
        InputStream inputStream;
        inputStream = socket.getInputStream();
        OutputStream outputStream = socket.getOutputStream();
        //客户端向socket发送数据
        PrintWriter printWriter = new PrintWriter(outputStream);
        //printWriter.println("zj+202007239999893+133070303805451+86+0+2031+0+93+20211111153254+1.0#1.0#11600+0+end");    //没有注册
        //printWriter.println("hm+869401040126203+0+7+end");//没有注册
        //printWriter.println("zj+202007239999892+133070300000099+77+0+0+0+96+2021109916" + i +"33+1.0#1.0#9383+0+end");
        printWriter.println("zj+202007239999856+000000000000078+97+0+1975+0+62+20220105103307+1.0#1.0#12160+0+end");
        printWriter.flush();
        //读取数据
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String readLine = bufferedReader.readLine();
        System.out.println(readLine);
        inputStream.close();
        outputStream.close();
        socket.close();
    }
}
