package com.ics.common.utils.device;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class MenjinUtils {
    public static void main(String[] args) {
        try {
            // 创建TCP服务器并绑定到指定端口   //tcp 通讯接口4370
            int port = 8083;
            ServerSocket serverSocket = new ServerSocket(port);
            System.out.println("服务器已启动，等待客户端连接...");

            while (true) {
                // 等待客户端连接
                Socket clientSocket = serverSocket.accept();
                System.out.println("客户端连接成功");

                // 创建输入流和输出流
                InputStream inputStream = clientSocket.getInputStream();
                OutputStream outputStream = clientSocket.getOutputStream();

                // 读取客户端发送的数据
                byte[] buffer = new byte[1024];
                int bytesRead = inputStream.read(buffer);
                String receivedMessage = new String(buffer, 0, bytesRead);
                System.out.println("收到消息：" + receivedMessage);

                // 发送响应数据给客户端
                String responseMessage = "Hello, Client!";
                outputStream.write(responseMessage.getBytes());

                // 关闭连接
                clientSocket.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}



