shichongfu
2023-10-08 6f83c8971714e2384df45706e364ed45a0cd1183
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.product.device.data.gather.service;
 
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
 
import com.product.core.dao.BaseDao;
import com.product.core.spring.context.SpringBeanUtil;
import com.product.core.spring.context.SpringMVCContextHolder;
import com.product.device.config.DeviceConst;
 
 
 
public class DeviceDataThread extends Thread {
    
 
    //服务器状态
    public static boolean serverStatus=false;
    
    @Override
    public void run() {
        start();
    }
    
    public void start() {
        ServerSocket serverSocket = null;
        try {
            // 建立一个服务器Socket(ServerSocket)指定端口并开始监听
            serverSocket = new ServerSocket(DeviceConst.DEVICE_DATA_SOCKET_PORT);
            serverStatus=true;
            SpringMVCContextHolder.getSystemLogger().info("=====建立数据接收服务,端口"+DeviceConst.DEVICE_DATA_SOCKET_PORT);
            // 监听一直进行中
            while (true) {
                // 使用accept()方法等待客户发起通信
                Socket socket = serverSocket.accept();
                DeviceDataProcessThread thread = new DeviceDataProcessThread(socket,SpringBeanUtil.getBean(BaseDao.class));
                thread.start();
            }
        } catch (IOException e) {
            serverStatus=false;
            e.printStackTrace();
        }
    }
}