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


static arraylist<client> clients = new arraylist<client>();     while (true)             {                                                               socket s = server.accept();                 system.out.println("client connected " + s.getlocaladdress().gethostname());                    thread t = new thread(new client(s));                 t.start();                                 } 

simply premise, inside client class got made adding static arraylist of 'client' located in main server class (above) i.e.

clients.add(client.this); 

i every 10 seconds, sending online users object clients in arraylist (global message in effect)

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

now, correctly add right number of clients etc.. , list gathered correctly, client happily recieves list every 10 seconds, until, client connects server, happens, first client stops receiving list , new 1 takes it's place, getting '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 adding client:

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

and

clients.add(this); 

in client itself

please copy actual declaration of "out", guess static , shared between instances of class. give symptoms describe.


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -