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
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
}
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 :)
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
ASKER
how do i Iterate thru all client handlers?
Keep their references in a List/Set as they connect to server
ASKER
have any example link, found nothing in google?
ASKER
so i need a Vector to store the threads?
yes, or some other suitable collection
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.