聊天:向服务器上的所有客户端发送消息
Chat: Sending message to all clients on a server
我有一个服务器-客户端应用程序集。 (家庭作业)
到目前为止,我已经想出了如何让多个客户端连接到服务器并让服务器聚合客户端发送的消息,以及让服务器将客户端的消息发送回客户端并将其显示在聊天面板。
我的问题是尝试向多个客户端发送消息。 我只能使用 ServerSocket 和 Socket 库。
假设我有 2 个客户端连接到服务器。一个客户端发送消息,该消息显示在客户端的聊天中。第二个客户端发送消息,第一个客户端未收到消息,第二个客户端的聊天 window 显示第一个客户端的消息。
基本上服务器发送的是各自客户端未在聊天框中显示的最新消息,我不知道为什么或如何。
服务器到客户端通信的代码:
Class CommunicationThread extends Thread {
//Vector containing all client sockets currently connected
//Held offsite, here for clarity
public Vector<Socket> socketVector;
public CommunicationThread (Socket clientSoc, Server ec3, Vector<Socket>socketVectorg)
{
//add new socket to vector, start thread
clientSocket = clientSoc;
socketVectorg.add(clientSocket);
this.socketVector = socketVectorg;
gui = ec3;
}
public void run()
{
System.out.println ("New Communication Thread Started");
try {
//Client's chat box (output)
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(),
true);
//Input line from client
BufferedReader in = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println("Server: " + inputLine);
gui.history.insert(inputLine + "\n", 0);
//*************HERE IS MY ISSUE*******************
for(Socket s : socketVector){
out = new PrintWriter(s.getOutputStream(),
true);
out.println(inputLine);
}
if (inputLine.equals("Bye."))
break;
if (inputLine.equals("End Server."))
gui.serverContinue = false;
}
out.close();
in.close();
clientSocket.close();
}
catch (IOException e)
{
System.err.println("Problem with Communication Server");
//System.exit(1);
}
}
}
我知道我正在覆盖 "out",但我认为这不是我的问题,所以在我测试时它在我的代码中。
我的问题已在上面的代码中标记出来。该向量准确地存储了套接字 ID,并且由于我正在基于该向量创建一个新的 PrinterWriter,所以我只假设它会获得相应客户端的输出字段,但事实并非如此。
我的直觉是线程或关闭输出的问题,但老实说我不知道。
如有任何建议,我们将不胜感激。
在我看来,你的问题是你想在同一个地方对客户端套接字进行输入和输出工作,但没有必要这样做。客户端套接字的输出流可以在 GUI 线程中写入,并且全部集中在一个地方。如果需要,您可以保留输出流的集合,当您想要回复所有内容时,遍历集合(可能是 HashMap<String, OutpuStream>
,其中字符串是某个客户端标识符)并发送消息。
我有一个服务器-客户端应用程序集。 (家庭作业)
到目前为止,我已经想出了如何让多个客户端连接到服务器并让服务器聚合客户端发送的消息,以及让服务器将客户端的消息发送回客户端并将其显示在聊天面板。
我的问题是尝试向多个客户端发送消息。 我只能使用 ServerSocket 和 Socket 库。
假设我有 2 个客户端连接到服务器。一个客户端发送消息,该消息显示在客户端的聊天中。第二个客户端发送消息,第一个客户端未收到消息,第二个客户端的聊天 window 显示第一个客户端的消息。
基本上服务器发送的是各自客户端未在聊天框中显示的最新消息,我不知道为什么或如何。
服务器到客户端通信的代码:
Class CommunicationThread extends Thread {
//Vector containing all client sockets currently connected
//Held offsite, here for clarity
public Vector<Socket> socketVector;
public CommunicationThread (Socket clientSoc, Server ec3, Vector<Socket>socketVectorg)
{
//add new socket to vector, start thread
clientSocket = clientSoc;
socketVectorg.add(clientSocket);
this.socketVector = socketVectorg;
gui = ec3;
}
public void run()
{
System.out.println ("New Communication Thread Started");
try {
//Client's chat box (output)
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(),
true);
//Input line from client
BufferedReader in = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println("Server: " + inputLine);
gui.history.insert(inputLine + "\n", 0);
//*************HERE IS MY ISSUE*******************
for(Socket s : socketVector){
out = new PrintWriter(s.getOutputStream(),
true);
out.println(inputLine);
}
if (inputLine.equals("Bye."))
break;
if (inputLine.equals("End Server."))
gui.serverContinue = false;
}
out.close();
in.close();
clientSocket.close();
}
catch (IOException e)
{
System.err.println("Problem with Communication Server");
//System.exit(1);
}
}
}
我知道我正在覆盖 "out",但我认为这不是我的问题,所以在我测试时它在我的代码中。
我的问题已在上面的代码中标记出来。该向量准确地存储了套接字 ID,并且由于我正在基于该向量创建一个新的 PrinterWriter,所以我只假设它会获得相应客户端的输出字段,但事实并非如此。
我的直觉是线程或关闭输出的问题,但老实说我不知道。
如有任何建议,我们将不胜感激。
在我看来,你的问题是你想在同一个地方对客户端套接字进行输入和输出工作,但没有必要这样做。客户端套接字的输出流可以在 GUI 线程中写入,并且全部集中在一个地方。如果需要,您可以保留输出流的集合,当您想要回复所有内容时,遍历集合(可能是 HashMap<String, OutpuStream>
,其中字符串是某个客户端标识符)并发送消息。