package com.lovecoding.day22.tcp02;

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

//服务端
public class ServerTCP {
    public static void main(String[] args) throws IOException {
        //创建服务端 端口 0-65535
        ServerSocket serverSocket = new ServerSocket(8888);
        System.out.println("开启服务端");
        //使用线程
        while (true){
            Socket accept = serverSocket.accept();
            //开启线程
            new Thread( ()->{
                try {
                    //获取输入信息
                    InputStream is = accept.getInputStream();
                    byte[] bytes = new byte[1024];
                    int read = is.read(bytes);
                    System.out.println("ip-"+accept.getInetAddress().getHostAddress()+":" +new String(bytes,0,read));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }
}