Solved

Connect multiple Clients to SocketServer

Posted on 2006-10-31
15
198 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
0
Comment
Question by:Knightley
  • 4
  • 4
  • 3
  • +1
15 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 25 total points
ID: 17843880
>>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
0
 
LVL 14

Assisted Solution

by:hoomanv
hoomanv earned 25 total points
ID: 17844770
0
 
LVL 92

Expert Comment

by:objects
ID: 17848150
> 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.
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 1

Author Comment

by:Knightley
ID: 17848640
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 :)
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 17848650
Iterate thru all client handlers and write the same thing to all outputs
0
 
LVL 1

Author Comment

by:Knightley
ID: 17848696
how do i  Iterate thru all client handlers?
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 17848707
Keep their references in a List/Set as they connect to server
0
 
LVL 1

Author Comment

by:Knightley
ID: 17848741
have any example link, found nothing in google?
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 17848762
0
 
LVL 92

Expert Comment

by:objects
ID: 17848775
0
 
LVL 1

Author Comment

by:Knightley
ID: 17848866
so i need a Vector to store the threads?
0
 
LVL 92

Expert Comment

by:objects
ID: 17849069
yes, or some other suitable collection
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
splitOdd10 challenge 5 108
Cipher Configuration on Apache HTTPD 4 63
difference of if loops 23 49
HashTable highest marks enumeration alternative 9 39
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question