We help IT Professionals succeed at work.

Connect multiple Clients to SocketServer

Knightley
Knightley asked
on
Medium Priority
241 Views
Last Modified: 2010-03-31
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
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016
Commented:
>>or do i have to initiate an Array to store all the ClientWorkers so each time a new Client is created?

If you need to keep the reference to each, yes. You could make them free-running so you don't need one

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Top Expert 2006
Commented:
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> 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.

Author

Commented:
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 :)
Top Expert 2006

Commented:
Iterate thru all client handlers and write the same thing to all outputs

Author

Commented:
how do i  Iterate thru all client handlers?
Top Expert 2006

Commented:
Keep their references in a List/Set as they connect to server

Author

Commented:
have any example link, found nothing in google?
Top Expert 2006

Commented:
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:

Author

Commented:
so i need a Vector to store the threads?
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
yes, or some other suitable collection
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.