gpt4 book ai didi

java - 服务器不向连接到它的客户端发送消息

转载 作者:行者123 更新时间:2023-12-01 11:32:33 25 4
gpt4 key购买 nike

我创建了一个多线程聊天服务器,当客户端连接时,它会启动一个单独的线程供它们进行通信。问题是我的服务器没有将从一个客户端收到的消息广播到所有其他连接的客户端,我根本不知道如何实现它。以下是我的代码,请随意编辑:

服务器类:

ArrayList<ServerThread> clientlist  = new ArrayList();
Hashtable clientList = new Hashtable();
private static ArrayList<PrintWriter> writers = new ArrayList<PrintWriter>();


public ChatServer(JTextField jtfA, JTextField jtfB, JTextArea jtaA, JTextArea tapane3) {
// TODO Auto-generated constructor stub
address= jtfA;
GETPORT=jtfB;
PORT=Integer.parseInt(jtfB.getText());
displaytext=jtaA;
clientside=tapane3;

}


public void run() {
// TODO Auto-generated method stub

try {
InetAddress ad=InetAddress.getLocalHost();

String ip=ad.toString();


address.setText(string);
address.setEditable(false);


System.out.println("Server IP is: " + ad);
ss = new ServerSocket (PORT);

Socket cs = null;
while (true)
{

cs=ss.accept();
System.out.println("Connected"+clientlist);

ServerThread handler = new ServerThread(cs,displaytext,clientside,writers);
clientlist.add(handler);
handler.start();
System.out.println(writers);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}


@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub


if(e.getActionCommand().equals("DisConnect"))
{
try {
ss.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}

服务器线程类::

public class ServerThread extends Thread {

Socket client;
JTextArea display;
JTextArea clients;
ServerSocket server;
ArrayList<PrintWriter> writers ;
public ServerThread(Socket cs, JTextArea displaytext, JTextArea clientside, ArrayList<PrintWriter> writers) {
// TODO Auto-generated constructor stub

client=cs;
display=displaytext;
clients=clientside;
this. writers=writers;



}
public void run () {


try{
BufferedReader br = new BufferedReader(
new InputStreamReader(
client.getInputStream()));
PrintWriter pr = new PrintWriter(
new OutputStreamWriter(
client.getOutputStream()));


String clientMsg="";
boolean all;
do {


//read msg from client

clientMsg = br.readLine();
//display.setText(clientMsg);
System.out.println("Server Received: " + clientMsg);
pr.println(clientMsg);
display.append(clientMsg+"\n");
clients.append(clientMsg+"\n");
pr.flush();

} while((all=br.readLine() != null));


br.close();
pr.close();


while (true) {
String input = br.readLine();
if (input == null) {
return;
}
for (PrintWriter writer : writers) {
writer.println("MESSAGE " + ": " + input);
}

}}
catch (UnknownHostException uhe){

System.out.println("Server issue");


}
catch (IOException ioe){

System.out.println("Server Error IO");

}
catch (Exception e){

System.out.println("Server Error General");

}}
}

最佳答案

错误的 do/while 循环。您丢弃了每一行,并且没有正确检测流结束。应该是:

while ((line = br.readLine()) != null)

关于java - 服务器不向连接到它的客户端发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30287480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com