Reputation: 271
I created a java server using the socket method, but I could make it connect to 1 client at a time.
My question is how do I make my server to connect to each client separately and communicate with each client equally so you know like 4 clients could use the app without being affected by other users but they could still use the server's service.
Upvotes: 1
Views: 274
Reputation: 59950
You can use Threads
, so each Thread
can create a new connection with a client X like this :
Thread thread = new Thread() {
public void run() {
CreateConnectionToYourClient();
}
};
thread.start();
Hope this can help you
Upvotes: 1
Reputation: 4039
Make a Client object that holds the informatiom about the client (name, socket, additional infos required by your program), inside the main loop's class create an arraylist of clients.
When a new client joins, create a new client object and put him inside the arraylist. Loop through each client and update them if needed. You'll also need to check if they disconnected or not and remove them if they are. Because of this I advise you to loop through the clients from the last to the first, because then removing am object doesn't make another one get missed.
The object things is not necessary if you don't need to have additional info, just the socket, but it doesn't hurt.
Upvotes: 0