?
Solved

how to add a server thread

Posted on 2003-12-04
15
Medium Priority
?
247 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

Independent Software Vendors: 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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month16 days, 9 hours left to enroll

862 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