-1

I'm working in a chat room like project and I'm trying to figure out how to receive messages from other users in the chat room whenever they send them. here is what I did

class ListenFromServer extends Thread {

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

and bin is

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

and the console is just to append the message to the chat JTextArea.

The only problem with this code is that my program is just stuck in the and doesn't do anything else although it's in a thread.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Have you made sure that the server terminates the string it sends with either `\n` or `\r`. If the string is not terminated by one of these characters the `BufferedReader` is stuck in an endless loop. Can you provide code on how your server is sending and receiving data? – L.Spillner May 03 '18 at 08:44

2 Answers2

0

BufferedReader.readLine() doesn't throw an IOException when the peer has closed the connection. It returns null, and you need to test for that.

The only problem with this code is that my program is just stuck in the [read loop]

Because you are ignoring the EOS condition.

and doesn't do anything else although it's in a thread.

Irrelevant.

user207421
  • 305,947
  • 44
  • 307
  • 483
-1

I guess you'll need to setup a Database where you want to register every single message send from the clients while updating said clients to make sure they get the last messages added to the database.

You're going to need JSON as it's probably the best way to exchange informations between server and client, here are some functions you should look at to handle the JSON parsing part.

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().

As for the Database connexion and interaction part it can differ depending on what you chose to use and how you chose to do it (you could directly access your DB and it's data or use HTTP calls using the HttpClient Java class to recover the data the same way as you'll do with an API), there is plenty of tutorial, i'am gonna update this with some links (can't remember them at the moment).

Hope this will help guiding you own researches. The rest is up to you.

N.K
  • 1,601
  • 2
  • 19
  • 31
  • 'The only problem with this code is that my program is just stuck', and there is exactly nothing here that addresses it. – user207421 May 03 '18 at 23:32
  • I'am sorry then, i would still suggest you to read the post again, it's kinda unclear (at least for me) what he is asking between the title and the problems he is pointing out in the begining and the end of the question. I had an hard time figuring out wich problem he was really asking for a solution. Should have kept myself from answering, i'll remember that anyways. – N.K May 04 '18 at 07:55
  • Come off it. What part of 'the only problem with this code is ...' don't you understand? The only question here is whether you have read the question at all. The OP states the problem in the part I quoted: I have answered it: you haven't. – user207421 May 04 '18 at 10:00
  • "I'm trying to figure out how to receive messages from other users" is what i focused on, and this is what i tried to answer, even tho i totally agree with what you said i wanted to make it clear, i'am not saying you are wrong nor am i saying that your downvote is unfair (as i feel it's what you understood looking at the tone you employed), i just wanted to explain myself and the reasons of my mistake that's all. cheers anyway :) – N.K May 04 '18 at 11:19