Solved

Connect multiple Clients to SocketServer

Posted on 2006-10-31
15
201 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

705 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