目录

Socket通信示例Demo

目录

Socket通信示例Demo

Socket网络通信

服务器端:


/**
 * BIO : 阻塞io
 **/
@Data
public class BioServer {

    private ServerSocket serverSocket;

    private static ThreadPoolExecutor threadPool = new ThreadPoolExecutor(10, 20, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<>(10));

    public void init(int port) throws IOException {
        serverSocket = new ServerSocket(port);
    }

    public void read() {
        if (serverSocket == null) {
            return;
        }
        Socket socket = null;
        while (true) {
            try {
                socket = serverSocket.accept();
                // 交给线程池处理,避免遗漏其他客户端的请求
                threadPool.execute(new RequestHandler(socket));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Data
    class RequestHandler implements Runnable {
        private Socket socket;

        public RequestHandler(Socket socket) {
            this.socket = socket;
        }

        @Override
        public void run() {
            InputStream inputStream = null;
            try {
                inputStream = socket.getInputStream();
                byte[] bytes = new byte[10240];
                StringBuffer sb = new StringBuffer();
                int len = 0;
                while ((len = inputStream.read(bytes)) != -1) {
                    sb.append(new String(bytes, 0, len, "UTF-8"));
                }
                System.out.println("msg from client : " + sb);
                inputStream.close();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    public static void main(String[] args) {
        try {
            BioServer readServer = new BioServer();
            readServer.init(8080);
            readServer.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

客户端;

@Data
public class BioSocket {

    private String host;
    private int port;

    public Socket init(){
        try {
            Socket socket = new Socket(host,port);
            return socket;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public BioSocket(String host,int port) throws IOException {
        this.host = host;
        this.port = port;
    }

    public void write(){
        while (true){
            Socket socket = null;
            OutputStream outputStream = null;
            try {
                socket = init();
                outputStream = socket.getOutputStream();
                Scanner scanner = new Scanner(System.in);
                String str = scanner.nextLine();
                outputStream.write(str.getBytes());
                outputStream.close();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        try {
            BioSocket writeClient = new BioSocket("localhost",8080);
            Thread writeThread = new Thread(writeClient::write);
            writeThread.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}