Plz help!!!! plz!!!! problem with multithreaded server in java
Hi all,
I am in a great problem. I am trying to implement a multithreaded server using java, first look at the code...
Code:
/*------- Main Server (server.java)--------------*/
import java.io.* ;
import java.net.* ;
public class server
{
public static void main(String args[]) throws IOException
{
ServerSocket s = new ServerSocket(1234);
while(true)
{
Socket s1 = s.accept();
worker w = new worker(s1);
w.run();
}
//s.close();
}
}
this server creates a thread when a client wants to establish a connection with it, the thread manage all things for that client, so I named this thread as "worker", here comes the code for worker...
Code:
/*--------- worker (worker.java) -------------*/
import java.io.* ;
import java.net.* ;
public class worker implements Runnable
{
private DataInputStream datain;
public worker(Socket s) throws IOException
{
InputStream in = s.getInputStream();
datain = new DataInputStream(in);
}
public void run()
{
try{
while(true)
{
String st = new String(datain.readUTF());
System.out.println(st);
}
}
catch(IOException ioe)
{
System.out.println(ioe);
}
}
}
and I have three clients, client1.java, client2.java and client3.java
they are...
Code:
/*---------- client1.java ----------------*/
import java.io.* ;
import java.net.* ;
public class client1
{
public static void main(String args[]) throws IOException
{
Socket s = new Socket("localhost", 1234);
OutputStream out = s.getOutputStream();
DataOutputStream dataout = new DataOutputStream(out);
try{
while(true)
{
Thread.sleep(1000);
dataout.writeUTF("client1");
System.out.println("client1");
}
}
catch(InterruptedException ie)
{
System.out.println(ie);
}
//dataout.close();
//out.close();
//s.close();
}
}
/*----------- client2.java-------------- */
import java.io.* ;
import java.net.* ;
public class client2
{
public static void main(String args[]) throws IOException
{
Socket s = new Socket("localhost", 1234);
OutputStream out = s.getOutputStream();
DataOutputStream dataout = new DataOutputStream(out);
while(true)
{
dataout.writeUTF("client2");
System.out.println("client2");
}
//dataout.close();
//out.close();
//s.close();
}
}
/*--------------- client3.java ------------*/
import java.io.* ;
import java.net.* ;
public class client3
{
public static void main(String args[]) throws IOException
{
Socket s = new Socket("localhost", 1234);
OutputStream out = s.getOutputStream();
DataOutputStream dataout = new DataOutputStream(out);
try{
while(true)
{
Thread.sleep(2000);
dataout.writeUTF("client3");
System.out.println("client3");
}
}
catch(InterruptedException ie)
{
System.out.println(ie);
}
//dataout.close();
//out.close();
//s.close();
}
}
the problem is, that, when start client1, the thread1 from the server starts to serve this client, and when I start another client client2, this thread does not start, until client1 and thread1 get finished. Same thing happens when I start 3 threads simultaneously, but the server and the workers supposed to serve all the clients at the same time, but it is working on "first come first serve" basis.
What will I do?........
I need concurrent workers!!!!
|