Solved

how to add a server thread

Posted on 2003-12-04
15
236 Views
Last Modified: 2010-03-31
Below is code that implements a thread on a server when a client connects. Im trying to modify the code as to accept a connection from two clients and handle each connection(client) in a different thread.

//this is the actual code in the server
 serve = new ServerSocket( 5004, 1000 ) ;
   
 new ClientThread(serve.accept());

   }
   catch( IOException e )
  {
   e.printStackTrace() ;
  }

 } //end while

class ClientThread extends Thread {
   private Socket socket;
 

   public ClientThread(Socket socket) {
     this.socket = socket;
     start();
   
   }


//this is what im trying to implement, this is not part of the code in the server
thread1 = new ClientThread(serve.accept());
thread2 = new ClientThread(serve.accept());

public ClientThread(Socket socket) {
     this.socket = socket;
    System.out.println("start");
     thread1.start();
     System.out.println("stop");
     thread2.start
0
Comment
Question by:eire_ireland
  • 9
  • 3
  • 3
15 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9877140
It's already doing that! Every time a client connects, a new thread is created and started.
0
 
LVL 92

Expert Comment

by:objects
ID: 9877155
> new ClientThread(serve.accept());

you are already creating a thread you just need to start it:

new ClientThread(serve.accept()).start();
0
 
LVL 92

Expert Comment

by:objects
ID: 9877163
>    System.out.println("start");
>     thread1.start();
>     System.out.println("stop");
>     thread2.start

You don't need to do that
0
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 92

Expert Comment

by:objects
ID: 9877170
0
 
LVL 92

Expert Comment

by:objects
ID: 9877184
Alternatively you could start it in the ctor:

public ClientThread(Socket socket) {
     this.socket = socket;
     start();
}

But I'd suggest starting as I posted above, allowing the main thread to control when new threads are started.
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 25 total points
ID: 9877314
You're already starting it in the ctor if this is referring to code at

http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20815051.html
0
 

Author Comment

by:eire_ireland
ID: 9877399
ya, thats where its referring to, the reason I think its only starting one thread is cos when I connect with a second client it does'nt the server does'nt output the string sent by the second client.
0
 

Author Comment

by:eire_ireland
ID: 9877437
correction : "it does'nt" should'nt be there after client
0
 
LVL 92

Expert Comment

by:objects
ID: 9877497
you only need one thread.
0
 
LVL 92

Expert Comment

by:objects
ID: 9877505
meaning one thread per client.
0
 
LVL 92

Expert Comment

by:objects
ID: 9877616
> output = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);

no need to do that every loop, you should move that call outside the loop.
put it after where you define in.
0
 
LVL 92

Accepted Solution

by:
objects earned 25 total points
ID: 9877652
your problem is you don't loop waiting for new connections:

while (true)
{
   new ClientThread(serve.accept());
}
0
 

Author Comment

by:eire_ireland
ID: 9882468
Cheers fellas
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9882491
:-)
0
 
LVL 92

Expert Comment

by:objects
ID: 9885105
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
groupSum6 challenge 6 86
GUI builder for Eclipse? 8 35
Why method in Java which is called from Runnable run() doesn't need to be 'static'? 1 24
Java exception bubble up 2 18
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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 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…

773 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