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