• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem Regd. Handling Multiple Clients

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I have a Threaded Java server, which needs to access multiple clients.
The server is started at a port specified from another class.I invoke the constructor of the Java server only once and start the server at the specified port.Now,i would like my server to listen continuously to my client, whenever the client sends a request.
The code is attached below:
<CODE>
import java.io.*;
import java.net.*;

public class JavaServer extends Thread{
static int port;
Dispatcher dispatch;
ServerSocket server_socket;
Socket s;


/**
* Deafult constructor
*/
public JavaServer(){ }
/**
* Overloaded Constructor,starts the server at the specified port
* @param p int
* @param dispatch Dispatcher
*/
public JavaServer(int p,Dispatcher dispatch) {
this.dispatch=dispatch;
port = p;

try {
System.out.println("before Socket port..");
server_socket = new ServerSocket(port);
System.out.println("Server started on port " +port);
}
catch (IOException io) {
System.out.println(" IO Exception " + io);
io.printStackTrace();
}
while(true){
while(this.acceptConn()){
System.out.println( "**** Client's connection accepted !! *** " );
start();//<b> $$$$$$$$$$$$$$$$$$I get the error java.lang.IllegalThreadStateException here </b>$$$$$$$$$$$$$$$$$$$$$
}
}
}

public boolean acceptConn(){

System.out.println("within accept........");
boolean bool=false;
try {
s = server_socket.accept();
System.out.println(
"server side i/p and o/p stream associated with socket...");
bool=true;
}
catch (UnknownHostException io) {
System.out.println(" Unknown host " + io);
io.printStackTrace();
}

catch (IOException io) {
System.out.println(" IO Exception " + io);
io.printStackTrace();
}
return bool;
}
/**
* This method is invoked when the Receiver is started.
*/
public void run(){
DataOutputStream dos = null;
int i = 0;
InputStream is = null;
BufferedReader bf = null;

try {
dos = new DataOutputStream(s.getOutputStream());
is = s.getInputStream();
bf = new BufferedReader(new InputStreamReader(is));
System.out.println("Waiting for response.." + bf);
long start=System.currentTimeMillis();
System.out.println(" **** Start time is **** "+ start);
dispatch.dispatchMessage(bf,start);
}

catch (IOException io) {
System.out.println(" IO Exception " + io);
io.printStackTrace();
}

}//end of run()

}//end of class
</CODE>
I understand that start() is invoked only once in the lifecycle of Thread.But,now,i have to invoke it multiple times.How is it done??
Thanks!!
[ April 29, 2004: Message edited by: Priya Vasudevan ]
[ April 29, 2004: Message edited by: Priya Vasudevan ]
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Priya,
U have just 1 thread right now, which u r trying to use for all the connections. Since u try to start the same thread more than once, it gives the exception.
Solution is :
Create a new thread once u have established the connection with the client. U can do this by creating an inner class and move the ref "s" into this class instead of having it in the JavaServer class. This is becoz u can't share the same client instance between all the threads. The ref s can point to different client after each connection.
It can be something like this

Then in the JavaServer class u can do the following:

I hope this clears your doubt.
Regards,
Gopals.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's exactly how you usually see this stuff. Here's a live example from a Wiki I wrote:
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic