java 服务器线程池测试

ncat 有个允许多客户端连接的参数,默认是100,我用线程池实现了类似的功能。
创建服务器线程池大小为100,可以同时处理100个连接,第100个往后的客户端需要等待前100个客户端有退出,它才能连上。
Server.java

import java.io.*;
import java.net.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Server {
    static ExecutorService executorService;
    static ServerSocket serverSocket;
    static Socket socket;
    static SocketAddress socketAddress;

    public static void main(String[] args) throws IOException {
        socketAddress = new InetSocketAddress(InetAddress.getByName("0.0.0.0"), 6000);
        serverSocket = new ServerSocket();
        serverSocket.bind(socketAddress);
        executorService = Executors.newFixedThreadPool(100);

        // 监听线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    while (true) {
                        socket = serverSocket.accept();
                        System.out.println(socket.getRemoteSocketAddress().toString());
                        Thread clientThread = new ClientThread(socket);
                        executorService.execute(clientThread);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}

class ClientThread extends Thread {
    static int num = 0;
    Socket socket;

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

    @Override
    public void run() {
        try {
            DataInputStream in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
            DataOutputStream out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
            String s = "你是第 " + (++num) + " 个客户端\n";
            out.write((s.getBytes()));
            out.flush();
            byte[] buffer = new byte[1024];
            while ((in.read(buffer)) != -1) {
                System.out.println(new String(buffer));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Client.java

import java.io.*;
import java.net.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Server {
    static ExecutorService executorService;
    static ServerSocket serverSocket;
    static Socket socket;
    static SocketAddress socketAddress;

    public static void main(String[] args) throws IOException {
        socketAddress = new InetSocketAddress(InetAddress.getByName("0.0.0.0"), 6000);
        serverSocket = new ServerSocket();
        serverSocket.bind(socketAddress);
        executorService = Executors.newFixedThreadPool(100);

        // 监听线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    while (true) {
                        socket = serverSocket.accept();
                        System.out.println(socket.getRemoteSocketAddress().toString());
                        Thread clientThread = new ClientThread(socket);
                        executorService.execute(clientThread);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}

class ClientThread extends Thread {
    static int num = 0;
    Socket socket;

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

    @Override
    public void run() {
        try {
            DataInputStream in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
            DataOutputStream out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
            String s = "你是第 " + (++num) + " 个客户端\n";
            out.write((s.getBytes()));
            out.flush();
            byte[] buffer = new byte[1024];
            while ((in.read(buffer)) != -1) {
                System.out.println(new String(buffer));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(Java)