?
Solved

how to add a server thread

Posted on 2003-12-04
15
Medium Priority
?
244 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

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…
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…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month14 days, 18 hours left to enroll

770 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