Solved

Connect multiple Clients to SocketServer

Posted on 2006-10-31
15
199 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
reverse digits of a number using for loop 5 52
jdbc error in jsp application 20 63
Desingning Refactoring existing code 2 26
Java: The Public Class Main 4 32
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

856 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