Solved

how to add a server thread

Posted on 2003-12-04
15
240 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
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 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

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
xampp tool 12 58
Is there a simpler dropbox system? 10 44
Is Applet the way to go for my drag and drop system? 8 35
Java Inheritance super keyword use 8 36
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn different types of Android Layout and some basics of an Android App.
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

749 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