This discussion is archived
11 Replies Latest reply: Feb 10, 2007 10:39 PM by 807597 RSS

Client recieving a User List

807597 Newbie
Currently Being Moderated
How would I make a client keep listening for a certain action..

Basically I have created a Client-Server Chat app... on connection and disconnection the server creates a userlist using the vectorlist, thus keeps updating on connection/disconnection of a socket/user.... so serverside is okay,

I've managed to make it send the vectorlist by serializing it over to a client upon connection from them.. So each time a new client connects they get the list.. So each client gets the list once meaning they won't be able to see other users on after them.. as the list gets updated on each connection on serverside.

I can make the server send the list on each connection or disconnection to each user.. but how will the client know the server is sending it? any tips or ideas to help me around this problem.

Thanks
  • 1. Re: Client recieving a User List
    800351 Newbie
    Currently Being Moderated
    The server could send the list to all the connected cllients each time new connection comes or an existing connection leaves. Design and implement a simple application protocol of your own. An application protocol is a meta-conversation between server and client. User interface design should also be important for it to work cleanly.
  • 2. Re: Client recieving a User List
    807597 Newbie
    Currently Being Moderated
    Okay I may be wrong but i think wat you meant i've done.. as thats what I thought too basically the server sends a list when each connection comes and an existing connections leaves.

    The problem is with the Client side.. how do I make it accept the list in the middle of a task. or randomly, basically I need the Client listening for a list at all times.. asa connection can be made anytime..

    At the moment when a client connects it will recieve a list.. because on connection server sends list client reads list.. it is told to specially check the stream because data has just been sent.. after which the next code loads up which is chatting etc.. and hten lets say another user connects, he will get the list the same way.. but his list will be updated with previous users.. so now how will the otehr user get that list.. I hope its making sense.. as its hard to explain.
  • 3. Re: Client recieving a User List
    800351 Newbie
    Currently Being Moderated
    how do I make it accept the list in the middle of a task
    Design and implement your own application protocol.

    Just a primitive example:
    // client input process
    if (srvMessage.equals("***I WANNA UPDATE USER LIST***"){
      getAndShowTheList();
    }
    else if ....
      ...
    }
      ...
  • 4. Re: Client recieving a User List
    807597 Newbie
    Currently Being Moderated
    Thanks,,

    okay so basically its similar to adding liking a prefix maybe to know what is being recieved and based on that.. do so and so..

    correct?

    so as it stands the way my client works is similar to what you've wrote it
    checks for pub and priv msg and if i was to add what you've siad it'd be something like..
    While (srvMessage = !null) {
    if (srvMessage.equals("pub"){
       pubRecieved();
    }
    if (srvMessage.equals("priv"){
       privRecieved();
    }
    
    if (srvMessage.equals("***I WANNA UPDATE USER LIST***"){
      getAndShowTheList();
    }
    }
    along those lines right?
  • 5. Re: Client recieving a User List
    800351 Newbie
    Currently Being Moderated
    The server would send ordinary user messages and its own meta messages which comprise your application protocol. If a user message happens to be equal to a server meta message, you should use a special handling for it on the server side.

    Here's a pseudo code (the server MUST use PrintWriter.println() with auto-flush):
    // reader is a BufferedReader
    try{
      while ((srvMessage = reader.readLine()) != null){
        if (META1){
          ...
        }
        else if (META2){
          ...
        ...
        else if (METAN){
          ...
        }
        else{ // ordinary user message
          showOrdinaryUserMessage(sevMessage);
        }
    ...
    ...
  • 6. Re: Client recieving a User List
    807597 Newbie
    Currently Being Moderated
    Okay awsome! i think i've got the jist.. let me give it a go.. and if no luck i'll come back.. thanks again :)
  • 7. Re: Client recieving a User List
    807597 Newbie
    Currently Being Moderated
    One point in regards to the PrintWrite comment you put? do you mean the messages the server sends to the client need to be in PrintWriter ?

    at the moment my server has the following
         outFromServer = new DataOutputStream(connection.getOutputStream());
         inToServer = new DataInputStream(connection.getInputStream());
         obj_out = new ObjectOutputStream (outToServer); //used to send vector
  • 8. Re: Client recieving a User List
    800351 Newbie
    Currently Being Moderated
    One of the important rules of thumb for network programming is refraining from reusing socket in/out stream among multiple high-level io stream classes. If your vector is a Vector<String>, then, simply sending them strings via PrintWriter is much simpler, safer and more robust. If the client absolutely needs a vector, it could make one from those strings quite easily. Or, more preferably, it could fill an exsisting vector with those strings. Vector class has methods for clearing and refilling its content.

    Don't use DataXxxxStream class for string io. They are almost deprecated since the advent of Reader/Writer family of classes.
  • 9. Re: Client recieving a User List
    807597 Newbie
    Currently Being Moderated
    Oh okay i get it! thanks for letting me know... so i should use printwriter for both the out and in.. or just out and for in bufferreader...

    So the Meta tag thing that i have to do myself as in making it check the string msg and maybe the first 3 letters are like identifers with a seprator i created myself? right.. because i got confused and thought the reason you said to use printwriter was because it had a special function for creating the Meta tag thing or identifers..
  • 10. Re: Client recieving a User List
    800351 Newbie
    Currently Being Moderated
    to use printwriter was because it had a special function for
    creating the Meta tag thing or identifers..
    No. Not at all. Sorry for having confused you.
    PrintWriter.println() always sends a '\n' at the end of line so client readLine() never hangs. But for that to work properly, you should set auto-flush on on the writer.
  • 11. Re: Client recieving a User List
    807597 Newbie
    Currently Being Moderated
    hehe it's okay it happens, i've confused many people before trying to explain my programming problems to them..

    Okay i got the reason for printwriter now.. will give that a go.. but as for the logic of like creating the meta tags... maybe like taking the first 3-4 letters of the string/msg and seperate it by maybe a ~ or similar something along those lines a good way to go about it...