gpt4 book ai didi

Java TCP 客户端-服务器连接 : always open to listen from server

转载 作者:太空宇宙 更新时间:2023-11-04 09:38:25 26 4
gpt4 key购买 nike

我正在使用 Swing 创建一个 Java 聊天。我几乎完成了基础知识,我只是无法将消息广播给每个客户端,不知何故,客户端没有收到消息。我有:

static Vector<ClientHandler> ar = new Vector<>();
static void Broadcast() throws IOException
{
for (int i = 0; i < ar.size(); i++)
{
try
{
DataOutputStream out = new DataOutputStream(ar.get(i).client.getOutputStream());

out.writeUTF(String.valueOf(ar.size()));
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}

效果很好,我已经连接了所有客户端等等。

我已经将客户端部分设为多线程,因此每个用户都可以拥有唯一的窗口和详细信息,我有这部分将文本发送到服务器端部分:

        public void actionPerformed(ActionEvent e) 
{
try
{
out.writeUTF(String.valueOf(clientSocket.getLocalPort()));
String resp = in.readUTF();

System.out.println(resp); // this only outputs for one client, is it not supposed to output it for every client on the thread, since this piece of code is shared with every other thread, and every other thread is waiting?
}
catch (Exception ex)
{
ex.printStackTrace();
}

}

提前谢谢您!

编辑:

对于那些想知道的人,我通过修复逻辑来修复这部分。似乎也不需要多线程客户端框架。我刚刚删除了多线程部分,它似乎工作正常。

最佳答案

这个问题的具体意义是什么?这是否意味着您无法确定这是服务器问题还是客户端问题?

关于Java TCP 客户端-服务器连接 : always open to listen from server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56228609/

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