简体   繁体   English

被动地在套接字上侦听聊天应用程序

[英]Passively listening on a socket for a chat application

I am currently developing a chat application which is quite basic overall, however I am encountering problems when receiving strings from both the client and the server side. 我目前正在开发一个非常基本的聊天应用程序,但是当从客户端和服务器端接收字符串时遇到问题。 I am using a thread to passively listen on the socket for incoming messages, which is where I suspect the problem to be. 我正在使用一个线程被动地监听套接字上的传入消息,这是我怀疑问题所在。 Am I doing this the right way? 我这样做是对的吗?

Source: Code for sending strings: 来源:发送字符串的代码:

send.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            messageBuffer = message.getText();
            out.println(messageBuffer);
            chat.append(username + ": " + messageBuffer + "\n");
            message.setText("");
        }
    });

I then have this which passively listens (problem is probably here): 然后我有这个被动地听(问题可能在这里):

public void run(){
    while(true){
        try {
               messageBufferIn = in.readLine();
               System.out.println(in.readLine());
               chat.append(recipient + ": " + messageBufferIn + "\n");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Im also calling the thread using this: 我也用这个调用线程:

public static void startChatting(){
    try {
        Thread.sleep(10);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

Thanks for any help you can provide, Im still new to threads overall so my mistake might be quite mediocre. 感谢您提供的任何帮助,我仍然是整体线程的新手,所以我的错误可能相当平庸。

Edit: The problem is when I try sending a message to the receiver, nothing comes through, I can confirm that they are connected. 编辑:问题是,当我尝试向接收器发送消息时,没有任何结果,我可以确认它们已连接。 In fact the System.out.println(in.readLine()); 实际上是System.out.println(in.readLine()); doesnt come through at all, not even a "null" output. 根本没有,甚至没有“空”输出。

  1. you have got issue with Concurency in Swing , Swing GUI don't know that you running / open the Socket on Background Task , 在Swing中遇到了Concurency的问题,Swing GUI不知道你在Background Task上运行/打开Socket

  2. all updates to the Swing GUI must be done on EDT , otherwise nothing happened or you got a few exceptions Swing GUI所有更新必须在EDT上完成,否则什么也没发生,或者你有一些例外

  3. have to wrap all updates from Background Task (in your case Runnable#Thread ) to the invokeLater for Swing GUI 必须将所有更新从Background Task (在您的情况下为Runnable#Thread )中包装到用于Swing GUI的invokeLater

  4. while(true){ is endless loop put there Boolean variable instead of true , then you can to stop, start or restart whatever in your case Socket while(true){被无限循环放在那里Boolean variable ,而不是true ,那么你可以停止,启动或重新启动任何在你的情况Socket

  5. in the case that send.addActionListener(new ActionListener(){ runs only once time (if user invoked by JButtons click) then to use SwingWoker for opening Socket, SwingWorker quite good guarantee that all output should be done on EDT 在该案件send.addActionListener(new ActionListener(){只运行一次的时间(如果用户通过调用JButtons点击),然后使用SwingWoker打开插座, SwingWorker相当不错的保证,所有的输出应该在美国东部时间完成

  6. difference betweens Runnable#Thread and SwingWorker is that SwingWorker is designated to run only once times Runnable#ThreadSwingWorker之间的区别是指定SwingWorker只运行一次

I suspect your main problem is that you're not flushing the OutputStream . 我怀疑你的主要问题是你没有刷新OutputStream You'll want to add out.flush() after you've finished writing to it and want to send the message. 在您完成写入并希望发送消息后,您将要添加out.flush() For example, your ActionListener would look like this: 例如,您的ActionListener将如下所示:

send.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
        messageBuffer = message.getText();
        out.println(messageBuffer);
        out.flush();
        chat.append(username + ": " + messageBuffer + "\n");
        message.setText("");
    }
});

If you don't do this, your OutputStream will sit there until it's buffer's full (which will be a lot of messages). 如果你不这样做,你的OutputStream将坐在那里,直到它的缓冲区已满(这将是很多消息)。

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

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