package cn.iocoder.yudao.module.system.util.socket;


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TCPServer {



    public static void main(String[] args) throws IOException {

        ServerSocket server=new ServerSocket(9000);
        while (true){
            // 监听客户端的请求,没有的话就会进行阻塞
            Socket socket=server.accept();
            //开启一个线程进行处理客户端的请求
            new  Thread(new Runnable() {
                @Override
                public void run() {
                    try {

                        InputStream is=socket.getInputStream();
                        byte[] bytes=new byte[1024];
                        int len=is.read(bytes);
                        System.out.println(new String(bytes,0,len));
                        OutputStream os= socket.getOutputStream();
                        //响应给客户端
                        os.write("收到谢谢".getBytes());
                        socket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }


                }
            }).start();
        }

    }

}

