繁体   English   中英

Java TCP客户端-服务器连接:始终打开以侦听服务器

[英]Java TCP Client-Server Connection: always open to listen from server

我正在使用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();
            }

        }

先感谢您!

编辑:

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

确切问题的重点是什么? 这是否意味着您无法弄清楚这是服务器问题还是客户端问题?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM