Java多线程客户端服务器:等待指定客户端数后启动线程实现消息广播
我把你写的多线程客户端-服务器聊天应用代码按规范整理好了哈~这个应用最多支持2个客户端连接,客户端得先验证密码才能加入,等连接数到上限后,服务器就会给每个客户端开个线程,专门处理消息读取和广播给其他在线客户端:
服务器端代码
import java.io.DataInputStream; import java.io.PrintStream; import java.io.IOException; import java.net.Socket; import java.net.ServerSocket; /* * A chat server that delivers public and private messages. */ public class Server_X_Client { // The server socket. private static ServerSocket serverSocket = null; // The client socket. private static Socket clientSocket = null; // This chat server can accept up to maxClientsCount clients' connections. private static final int numOfClients = 2; private static final clientThread[] threads = new clientThread[numOfClients]; private static final Socket[] sockets = new Socket [numOfClients]; public static void main(String args[]) throws InterruptedException { // The default port number. int portNumber = 2222; int count=0; /* * Open a server socket on the portNumber (default 2222). Note that we can * not choose a port less than 1023 if we are not privileged users (root). */ try { serverSocket = new ServerSocket(portNumber); } catch (IOException e) { System.out.println(e); } /* * Create a client socket for each connection and pass it to a new client * thread. */ while (true) { try { clientSocket = serverSocket.accept(); int i = 0; for (i = 0; i < numOfClients; i++) { if (sockets[i] == null) { PrintStream os = new PrintStream(clientSocket.getOutputStream()); DataInputStream in = new DataInputStream (clientSocket.getInputStream()); os.println("Enter your name: "); String name = in.readLine().trim(); os.println("Enter server password: "); String psw = in.readLine().trim(); if(psw.equals("Pelin")){ os.println("Password is correct+ "+ i+" pls wait.."); sockets[i] = clientSocket; }else{ os.println("Password is wrong.."); } break; } } if (i == numOfClients-1) { for (int j=0;j<numOfClients;j++){ if(threads[j]==null){ (threads[j] = new clientThread(sockets[j], threads)).start(); //threads[j].join(); } } } } catch (IOException e) { System.out.println(e); } } } } /* * The chat client thread. This client thread opens the input and the output * streams for a particular client, ask the client's name, informs all the * clients connected to the server about the fact that a new client has joined * the chat room, and as long as it receive data, echos that data back to all * other clients. When a client leaves the chat room this thread informs also * all the clients about that and terminates. */ class clientThread extends Thread { private DataInputStream is = null; private PrintStream os = null; private Socket clientSocket = null; private clientThread[] threads; private int maxClientsCount; public clientThread(Socket clientSocket, clientThread[] threads) { this.clientSocket = clientSocket; this.threads = threads; maxClientsCount = threads.length; } public void run() { int maxClientsCount = this.maxClientsCount; clientThread[] threads = this.threads; try { /* * Create input and output streams for this client. */ is = new DataInputStream(clientSocket.getInputStream()); os = new PrintStream(clientSocket.getOutputStream()); for (int i = 0; i < maxClientsCount; i++) { if (threads[i] != null && threads[i] != this) { threads[i].os.println("READY!!"); } } while (true) { String line = is.readLine(); if (line.startsWith("/quit")) { break; } for (int i = 0; i < maxClientsCount; i++) { if (threads[i] != null) { threads[i].os.println("Someone:" + line); } } } for (int i = 0; i < maxClientsCount; i++) { if (threads[i] != null && threads[i] != this) { threads[i].os.println("*** The user " +"XXX" + " is leaving the chat room !!! ***"); } } os.println("*** Bye "+" ***"); /* * Clean up. Set the current thread variable to null so that a new client * could be accepted by the server. */ for (int i = 0; i < maxClientsCount; i++) { if (threads[i] == this) { threads[i] = null; } } /* * Close the output stream, close the input stream, close the socket. */ is.close(); os.close(); clientSocket.close(); } catch (IOException e) { } } }
客户端代码(注:你提供的客户端代码不完整,以下是现有部分)
import java.io.DataInputStream; import java.io.PrintStream; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; import java.net.Socket; import java.net.UnknownHostException; import java.util.Scanner; public class NetworkClient implements Runnable { // The client socket private static Socket clientSocket = null; // The output stream private static PrintStream os = null; // The input stream private static DataInputStream is = null; private static BufferedReader inputLine = null; private static boolean closed = false; public static void main(Str
内容的提问来源于stack exchange,提问作者joy_jlee
相关产品推荐
相关产品推荐

