_Hey, I have a question. I want to write application which will have multiple chats, rooms etc.
And now i have some troubles with server. My conception is to store data of clients in Vector of ClientSocket classes.
ClientSocket class will be like this:
public class ClientSocket {
int client_id;
Socket socket;
}
On main server thread:
ServerSocket serverSocket;
Vector<ClientSocket> sockets;
And the idea is: when new connection is established, make a new thread, pass sockets vector as a parameter, generate player id, iterate through vector elements, check if client_id exists, if not, set the id, if yes, generate next and do the same.
And where is the problem? Im worried about synchronization. What will happen, if two clients arrive at same time, and vector size will change in the meantime? Am i doing it correct? Maby there is a better idea to organise it?
Thanks in advance
Marcin
// edit
I meant that situation, but i think i will use Peter Lawrey solution :

Since you need to look up the client by client_id I would use a Map. If you use a ConcurrentMap it will be thread safe.