gpt4 book ai didi

java - 如何让聊天程序服务器向所有连接的java客户端发送消息

转载 作者:行者123 更新时间:2023-12-02 01:00:19 25 4
gpt4 key购买 nike

我已经尝试制作这个程序三次,每次都更进一步。我的程序应该像这样工作:

客户端向服务器发送一条消息,然后服务器将该消息发送给所有连接的客户端(包括发送该消息的客户端)。当客户端收到消息时,他们会显示它。

我现在面临的问题是服务器仅将消息发送给发送消息的客户端,而其他客户端什么也没有收到。我认为发生这种情况是因为我接受客户的方式,或者至少与此有关

这是我当前的代码:

客户:

public class Client {

protected static JTextArea textArea = new JTextArea(20, 30);
protected static String sendMSG, getMSG;

public static void main(String[] args) throws IOException {
String hostName = args[0];
String Username = args[1];
boolean sending = true;

try (
Socket socket = new Socket(hostName, 1011);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
) {
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));

//frame setup
JFrame frame = new JFrame("chat client");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//text area
JScrollPane scrollPane = new JScrollPane(textArea);

//text field
JTextField MSGText = new JTextField(5);

//"send" button
JButton sMSGB = new JButton("send");
sMSGB.setPreferredSize(new Dimension(60, 30));
sMSGB.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent event) {
sendMSG = MSGText.getText();
MSGText.setText("");
out.println("<" + Username + ">: " + sendMSG);
}

});

//panel
JPanel p = new JPanel();
p.setLayout((new BoxLayout(p, BoxLayout.PAGE_AXIS)));
p.add(Box.createVerticalStrut(5));
p.add(scrollPane);
p.add(Box.createVerticalStrut(5));
p.add(MSGText);
p.add(Box.createVerticalStrut(5));
p.add(sMSGB);
p.add(Box.createVerticalStrut(5));
frame.getContentPane().add(p);

//set frame visible
frame.pack();
frame.setVisible(true);

System.out.println("<Client>: opened stream");

while(sending) {

/*while((sendMSG = stdIn.readLine()) != null) {
out.println("<" + Username + ">: " + sendMSG);
}*/

while((getMSG = in.readLine()) != null) {
System.out.println(getMSG);
textArea.append(getMSG + "\n");
//sending = false;
}
}
//sending = true;
}
}
}

服务器:

public class Server {

public static void main(String[] args) {
try (ServerSocket serverSocket = new ServerSocket(1011)) {
boolean listening = true;
while(listening) {
Socket socket = serverSocket.accept();
List<PrintWriter> outs = new CopyOnWriteArrayList<PrintWriter>();
new ServerThread(socket, outs).start();
System.out.println("opened thread");
}

} catch(IOException e) {
e.printStackTrace();
}
}
}

服务器线程:

public class ServerThread extends Thread {

private final Socket socket;
private final List<PrintWriter> outs;

public ServerThread(Socket socket, List<PrintWriter> outs) {
super("ServerThread");
this.socket = socket;
this.outs = outs;
System.out.println("Opened outs: " + outs.size());
}

private void sendToAll(String msg) {
for(PrintWriter out: outs) {
out.println(msg);
}

}

public void run() {
try (
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
) {
System.out.println("stream opened");
outs.add(out);
String getMSGs;

while((getMSGs = in.readLine()) != null) {
//out.println(getMSGs);
sendToAll(getMSGs);
System.out.println("msg received and sent");
}

} catch (IOException e) {
e.printStackTrace();
}
}
}

希望能帮到你。提前致谢

最佳答案

每次新客户端连接时,您都会重新初始化 outs:

            while(listening) {
Socket socket = serverSocket.accept();
List<PrintWriter> outs = new CopyOnWriteArrayList<PrintWriter>();
new ServerThread(socket, outs).start();
System.out.println("opened thread");
}

并且基本上生成带有空输出的线程。您需要将输出流(或套接字)列表保留在循环之外,并构建一种在更新 outs 列表(当新客户端连接时)时向现有线程发出信号的方法。

关于java - 如何让聊天程序服务器向所有连接的java客户端发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57774190/

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