[英]How to send messages to all chat users except sender in server/chat client program
我有一個聊天程序,可以向所有聊天用戶(包括發件人)發送消息。 它需要發送給除發件人以外的所有人。 此外,似乎已將每個聊天用戶的已發送消息發送到服務器一次。 例如,如果有三個聊天用戶,如果說“你好”,它將向服務器發送“你好”三次。 這是我的代碼,問題出在run方法上:
import java.io.*;
import java.net.*;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Set;
public class BroadcastChatServer {
private static Hashtable<String, PrintWriter> writers = new Hashtable<>(); //Hash table for Client Names and corresponding PrintWriter objects
private static Hashtable<Integer, String> clientNames = new Hashtable<>(); //Hash table for Client IDs and corresponding message received
private static ServerSocket ServerSock;
private static int PORT=1234;
public static void main(String[] args) throws IOException { //main method
try{
ServerSock=new ServerSocket(3456);
}
catch(IOException e){
System.out.println("can't listen on PORT");
System.exit(1);
}
do{
Socket client=null;
System.out.println("Listening for connection...");
try{
client=ServerSock.accept();
System.out.println("New client accepted");
ClientHandler handler=new ClientHandler(client);
handler.start();
}
catch(IOException e){
System.out.println("accept failed");
System.exit(1);
}
System.out.println("Connection successful");
System.out.println("Listening for input..");
}while(true);
}//ends main
private static class ClientHandler extends Thread {//Make the ClientHandler a class inside the main class as below
private Socket client;
private BufferedReader in;
private PrintWriter out;
public ClientHandler(Socket socket) {
client=socket;
try{
in=new BufferedReader(new InputStreamReader(client.getInputStream()));
out=new PrintWriter(client.getOutputStream(),true);
}
catch(IOException e){
e.printStackTrace();
}
}
public void run() {
try {
String received;
int message = 1;
do {
int index = 0;
received = in.readLine();
if (message == 1) {
String clientName = getName().substring(getName().length() - 1);
int clientNum = Integer.parseInt(clientName);
//add client ID and message received to the clientnames hash table
clientNames.put(clientNum, received);
System.out.println(clientNames.get(clientNum) + " has joined");
//add client name and corresponding PrintWriter to the writers hash table
writers.put(clientNames.get(clientNum),out);
//loop through the writers hash table and broadcast to all clients
//that a new client has joined
for (PrintWriter writer : writers.values()) {
writer.println( clientNames.get(clientNum) + " has joined");
}
message++;
} else {
String clientName = getName().substring(getName().length() - 1);
int clientNum = Integer.parseInt(clientName);
//Loop through the writers keySet, that is, (for String client:writers.keySet())
for (String client:writers.keySet()){
//broadcast to all clients except this one
System.out.println("Message from " + clientNames.get(clientNum) + ": " + received);
}
}
} while (!received.equals("BYE"));
}
catch (IOException e) {
System.out.println("failed");
System.exit(1);
}
}//ends run
}
}//ends broadcast chat server
看一下觀察者設計模式。 在那種情況下很好,並且很容易實現。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.