Client/Server chat, sending online user list, only received by the most recently connect client?

StackOverflow https://stackoverflow.com/questions/22585108

  •  19-06-2023
  •  | 
  •  

문제

static ArrayList<Client> clients = new ArrayList<Client>();    
while (true)
            {                                              
                Socket s = server.accept();
                System.out.println("Client connected from " + s.getLocalAddress().getHostName());  

                Thread t = new Thread(new Client(s));
                t.start();                   

            }

Simply premise, inside the Client class that just got made i am adding to a static ArrayList of 'Client' located in the main server class (above) i.e.

clients.add(Client.this);

I am then simply every 10 seconds, sending the currently online users as an object to all the clients currently in the ArrayList (Global message in effect)

 for(int i =0; i < clients.size(); i++)
                   {
                       System.out.print("sending list");
                       clients.get(i).sendList();
                   }

Now, it DOES correctly add the right number of clients etc.. and the list is gathered correctly, the client happily recieves this list every 10 seconds, UNTIL, another client connects to the server, as soon as this happens, the first client stops receiving the list and the new one takes it's place, getting all the 'received list' notifications. Whats going on here?

EDIT: sendList() code

public void sendList()
        {
            try 
            {
                ChatListObject list = new ChatListObject();
                list.setList(helper.getOnlineUsers());
                out.writeObject(list);
                out.flush();
            } 
            catch (IOException iOException) 
            {
                System.out.println(iOException);
            }
        }

Things tried for adding client:

                Client client = new Client(s);              
                Thread t = new Thread(client);
                t.start();                       
                clients.add(client);

and

clients.add(this);

in the client itself

도움이 되었습니까?

해결책

Please copy the actual declaration of "out", I would guess it is static and thus shared between instances of the class. That would give the symptoms you describe.

다른 팁

Can you show us the method of .sendList()?

Also make sure you are doing a few things

  • Make sure Client implements Serializeable
  • Also make sure you are flushing after every write to a client

I recommend you put

Client myClient = new Client(s);
clients.add(myClient);
Thread t = new Thread(myClient);

clients.add(Client.this);

This does not do what you think it does. You want to do:

clients.add(this);

or better yet, skip the static clients list and add the client when you create the object.

Here is a sample code on Java Server with Multiclient communication in the same context that I have already posted.

It might help you to understand it.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top