package com.warewms.socket;

import lombok.extern.slf4j.Slf4j;

import java.net.ServerSocket;
import java.net.Socket;

/**
 * 服务端监听类
 */
@Slf4j
public class SocketServerListenHandler {

    //这是一个不断等待获取网络传入请求的服务端Socket
    private ServerSocket serverSocket;

    /**
     * 构造方法
     * @param port 端口
     */
    public SocketServerListenHandler(int port) {
        try {
            serverSocket = new ServerSocket(port);
        } catch (Exception e) {
            log.error("Socket构造错误，errorMsg：" + e.toString());
        }
    }

    public void listenClientConnect() {

        while (true) {
            try {
                log.info("服务端监听开始...");
                Socket clientConnectSocket = serverSocket.accept();
                log.info("监听到客户端连接。。。创建处理客户端连接的handler工具。。");
                new SocketServerClientHandler(clientConnectSocket).start();
            } catch (Exception e) {
                log.error("服务端监听连接程序异常，errorMsg：" + e.toString());
            }
        }
    }
}
