Solved

Connect multiple Clients to SocketServer

Posted on 2006-10-31
15
196 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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now