Link to home
Start Free TrialLog in
Avatar of Knightley
Knightley

asked on

Connect multiple Clients to SocketServer

Am trying to connect multiple Clients to a Server, each Client is created inside
a Thread

while(true){
    ClientWorker w;
        try{
         w = new ClientWorker(server.accept());
         Thread t = new Thread(w);
         t.start();
     } catch (IOException e) {
         System.out.println("Accept failed: 4444");
     }
}

ClientWorker is a class with runnable interface, which handles InputStream and OutputStream.

So my question: Is the ClientWorker w created each time anew in the while (true) loop,
or
the the ClientWorker w will be overwritten each time the loop is run.

or do i have to initiate an Array to store all the ClientWorkers so each time a new Client is created?

thanx
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
> Is the ClientWorker w created each time anew in the while (true) loop,

yes, with the variable w referencing the new ClientWorker.

> the the ClientWorker w will be overwritten each time the loop is run.

w will reference the new instance, but the existing instance will still exist.
Avatar of Knightley
Knightley

ASKER

so i kept the code, and each client could well send some string to the server.

my problem is if i want to send some string to ALL the connected clients,
it does not work.

How could i tell the OutputStream to all the connected Clients?

thanx :)
Iterate thru all client handlers and write the same thing to all outputs
how do i  Iterate thru all client handlers?
Keep their references in a List/Set as they connect to server
have any example link, found nothing in google?
so i need a Vector to store the threads?
yes, or some other suitable collection