简体   繁体   中英

java chat (serversocket/socket)

I need to build an application Server module - a console application for communication clients on principle chat,the client module - a GUI application chat, which must be connected to the server. The problem is i need set limits on the number of connected users limits i get from configuration file whitch is read when the server starts

计算成功接受的数量,如果达到极限,则不再接受。

Why don't you put condition in your while loop?

try {

        int numOfConnectionLimit = 10; // or read number of connection from that config file

        ServerSocket listenerServ = new ServerSocket(servPort);
        System.out.println("Waiting...." + servPort + " "
                + "" + listenerServ.getInetAddress().getHostAddress());
        while (connectArray.size() < numOfConnectionLimit) {

            sock = listenerServ.accept();
            connectArray.add(sock);
            System.out.println("Client connected from: " + sock.getLocalAddress().getHostName());
            addUserName(sock);

            SocketChatServerReturn chat = new SocketChatServerReturn(sock);
            Thread X = new Thread(chat);
            X.start();

        }
    } catch (Exception exSock) {
        System.out.println("IOException on socket listen: " + exSock);
        exSock.printStackTrace();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM