0

正如标题所示,我正在实现一个小项目,它允许客户端使用多线程连接到服务器来管理连接。我想限制连接,当服务器充满请求时,其他客户端应该放在一个队列中,例如:服务器只允许 2 个客户端同时连接服务器,其他客户端直到轮到他们。

这是我的服务器类

public class Server {

    static BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
    //Declare an instance of CommandParameter class

    static CommandParameter par;
    //Initialize a BufferedReader and BufferedWriter
    static BufferedReader bufferedReader;
    static BufferedWriter bufferWriter;
    //
    static int port;
    static String serverData;
    static int proc_count;
    private static String command;

    public static void main(String[] args) throws Exception {
        command = userInput.readLine();
        System.out.println("Server is available");
        //Thread.sleep(2000);
        processCommand(command);
        startServer(port);

    }

    public static void startServer(int port) {
        ServerSocket serverSocket = null;
        try {
            //create a port for server in order to listen incoming connections
            serverSocket = new ServerSocket(port);
            //allow client connect to server

        } catch (IOException e) {
            System.out.println(e);
        }
        while (true) {
            try {
                new ThreadSocket(serverSocket.accept(),serverData).start();

            } catch (IOException ex) {
                System.out.println("Server starts fail");
            }
        }
    }

    public static void processCommand(String input) {
        //assign the user input to an String array
        String inputLine = input;
        int commandCount = checkCommand(inputLine);
        if(commandCount>3 || commandCount ==-1){
            System.out.println("Invalid command");
            System.exit(1);
        }
        //assign the user input to an String array
        String[] command = inputLine.split(" ");
        par = new CommandParameter();

        //JCommander parses the command to get the pairs parameter name and value
        JCommander jc = new JCommander(par, command);
        port = par.getPort();
        serverData = par.getServerdata();
        proc_count = par.getProc_count();
    }
    public static int checkCommand(String inputLine) {
        int count = 0;
        if (inputLine.contains("-port")) {
            count++;
        }else if (inputLine.contains("-data")) {
            count++;
        } else if (inputLine.contains("-proc_count")) {
            count++;
        } else{
            return -1;
        }

        return count;
    }
}

有任何想法吗?

4

2 回答 2

1

这正是异步 IO 最有用的地方。【大量 IO 请求需要由有限数量的工作线程处理】。大多数现代操作系统中都有系统调用允许异步 IO 操作。例如,Linux 中的 epoll,Windows、AIX 和 Solaris 中的 IOCP,BSD 风格和 Mac OS 中的 KQueue。在 Java 中,本地非阻塞调用被抽象在不同的​​ SPI 后面 - 示例 - sun.nio.ch.WindowsSelectorImpl、sun.nio.ch.KQueueSelectorImpl、sun.nio.ch.EPollSelectorImpl 等。

我推荐你使用的是Netty

使用较小的固定线程数为大量 HTTP 请求提供服务的示例是使用 netty API 的简单任务。

具有一个侦听器线程和两个工作人员的示例引导代码将如下所示

public static void setupHTTPListeners()
{
    ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(Executors.newFixedThreadPool(1),
    Executors.newFixedThreadPool(2)));
    -----
    -----
}   

在相同的上下文中,最受欢迎的读物之一是这里 - C10k 问题

于 2013-03-09T17:14:33.750 回答
0

Use ServerSocket(int port, int backlog); the backlog parameter is the maximum number of connections allowed.

于 2013-03-09T15:29:10.883 回答