A server with multiple clients
in
Core Library Questions
•
3 years ago
Hi! I have another question for you, friends...
I want to create a client-server app.
I have no idea on how to create a server that can handle multiple clients (10 or 100 or 1000 or up to infinity, with known limitations...).
And a client.... well, i suppose, i understand a client well, from the processing network lib example.. but not server.
Say, if my server has 1000 clients active in average... do i need to store them in an ArrayList? I would like to be aware of each client that I have connected and also to be able to detect new connecting clients and clients that disconnect. Well, I think you understand, somthing like a chatroom, where on the server side I would be able to track any client and know any of his info.
Also, all the clients that are present, must be processed (listened to their messages, executing the commands embedded in their messages and sending them back the global server world situation update)
For now I have:
- package newlandsserver;
- import java.util.ArrayList;
- import processing.core.PApplet;
- import processing.net.*;
- public class GameServer {
- PApplet parent;
- Server server;
- int port;
- UserInterface userInterface;
- ArrayList clients;
- GameServer(PApplet p_)
- {
- parent = p_;
- }
- void initialize(int port_)
- {
- port = port_;
- server = new Server(parent, port);
- }
- void update()
- {
- userInterface.update();
- clientsConnections();
- }
- void clientsConnections()
- {
- Client tempClient = server.available();
- if(!clientExists(tempClient))
- {
- clients.add(tempClient);
- }
- else
- {
- processClient( (Client)clients.get(getClientID(tempClient)));
- }
- }
- boolean clientExists(Client c_)
- {
- for(int i = 0; i < clients.size(); i++)
- {
- if (c_ == (Client)clients.get(i)) return true;
- }
- return false;
- }
- int getClientID(Client c_)
- {
- for(int i = 0; i < clients.size(); i++)
- {
- if (c_ == (Client)clients.get(i)) return i;
- }
- return -1;
- }
- void processClient(Client c_)
- {
- //client-processing stuff here
- }
- }
1