【问题标题】:Receiving messages from a server [duplicate]从服务器接收消息[重复]
【发布时间】:2018-05-03 07:58:27
【问题描述】:

我在一个类似项目的聊天室中工作,我试图弄清楚如何在聊天室中的其他用户发送消息时接收他们的消息。 这就是我所做的

class ListenFromServer extends Thread {

    public void run() {
        while(true) {
            try {
                 Console(bin.readLine());
            }
            catch(IOException e) {
                Console("Server has closed the connection: ");
                break;
            }
        }
    }
}

bin

InputStream in = s.getInputStream();
bin = new BufferedReader(new InputStreamReader(in));

控制台只是将消息附加到聊天JTextArea

这段代码的唯一问题是我的程序只是卡在了线程中并且没有做任何其他事情。

【问题讨论】:

  • 您是否确定服务器使用\n\r 终止它发送的字符串。如果字符串没有被这些字符之一终止,BufferedReader 就会陷入无限循环。您能否提供有关您的服务器如何发送和接收数据的代码?

标签: java network-programming chat serversocket


【解决方案1】:

BufferedReader.readLine() 在对等方关闭连接时不会抛出IOException。它返回 null,您需要对此进行测试。

这段代码唯一的问题是我的程序卡在了[读取循环]

因为你忽略了 EOS 条件。

虽然它在一个线程中,但不做任何其他事情。

无关紧要。

【讨论】:

    【解决方案2】:

    我想您需要设置一个数据库,您希望在其中注册从客户端发送的每条消息,同时更新所述客户端以确保它们获得添加到数据库中的最后一条消息。

    您将需要 JSON,因为它可能是在服务器和客户端之间交换信息的最佳方式,这里有一些您应该查看的函数来处理 JSON 解析部分。

    What is a JSON object : (just to be clear) A .json file contains text-based representations of data structures and objects. A JSON object is delimited by "{" and "}" and represent a single object/structure inside a .json file.
    
    JSONObject : It's a type representing a single JSON object in JAVA.
    
    JSONArray : Seems obvious, but it's an array wich contains JSONObject elements, 
                    i recommend you to use this to keep your objects packed into an array,
                    this type is providing a lot of usefull methods aswell as JSONObject does,
                    i'll list some of them below it's up to you to search for the others. 
    
    JSONArray.getJSONObject(index) : Pass it an index and it will return the JSONObject stored 
                                         at this index.
    JSONArray.put([value] | [index, value]) : This might be extremly straightforward but it's actually pretty 
                               usefull if you want to build a JSONArray, pass it a JSONObject 
                               or another common type (int, String, etc) and it will add it to 
                               your JSONArray. 
                               Depending on what you pass it you'll need an index aswell.
    
    JSONObject.keys() : Returns an iterator of the String names in your JSONObject.
    
    JSONObject.put(name, value) : Create a field in your JSONObject using the name you passed 
                                      and assigning the value to it.
    
    JSONObject.getString(name) : Pass the name of a field to it and it will return 
     it's value as a String. As you may have guess already, there is a couple of those, not only getString().
    

    至于数据库连接和交互部分,它可能会有所不同,具体取决于您选择使用的内容和选择的方式(您可以直接访问您的数据库及其数据或使用 HTTP 调用使用HttpClient Java class 来恢复数据的方式与使用 API 相同),有很多教程,我会用一些链接更新这个(暂时不记得它们了)。

    希望这将有助于指导您自己的研究。 剩下的就看你自己了。

    【讨论】:

    • '这段代码唯一的问题是我的程序卡住了',这里没有什么可以解决的。
    • 对不起,我仍然建议您再次阅读该帖子,有点不清楚(至少对我而言)他在标题和他在问题的开始和结束。我很难弄清楚他真正要求解决的问题。应该让自己不回答,反​​正我会记住的。
    • 放下它。 “这段代码的唯一问题是......”的哪一部分你不明白吗?这里唯一的问题是你是否读过这个问题。 OP 在我引用的部分中说明了问题:我已经回答了:你没有。
    • “我正在尝试弄清楚如何接收来自其他用户的消息”是我关注的重点,这也是我试图回答的问题,即使我完全同意你所说的我想要的说清楚,我不是说你错了,我也不是说你的反对票是不公平的(因为我觉得这是你所理解的,看着你使用的语气),我只是想解释一下我自己和我错误的原因就这样。无论如何都干杯:)
    猜你喜欢
    • 2021-03-30
    • 2018-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-17
    • 2020-06-15
    • 2018-04-25
    • 1970-01-01
    相关资源
    最近更新 更多