By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,830 Members | 1,722 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,830 IT Pros & Developers. It's quick & easy.

Plz help!!!! plz!!!! problem with multithreaded server in java

P: 6
Hi all,
I am in a great problem. I am trying to implement a multithreaded server using java, first look at the code...

Expand|Select|Wrap|Line Numbers
  1. /*------- Main Server (server.java)--------------*/
  2. import java.io.* ;
  3. import java.net.* ;
  4.  
  5. public class server
  6.  {
  7.    public static void main(String args[]) throws IOException
  8.     {
  9.       ServerSocket s = new ServerSocket(1234);
  10.       while(true)
  11.        {
  12.      Socket s1 = s.accept();
  13.                  worker w = new worker(s1);
  14.      w.run();
  15.        }    
  16.       //s.close();           
  17.     }    
  18.  }
  19.  
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...

Expand|Select|Wrap|Line Numbers
  1. /*--------- worker (worker.java) -------------*/
  2. import java.io.* ;
  3. import java.net.* ;
  4.  
  5. public class worker implements Runnable
  6.  {
  7.    private DataInputStream datain;        
  8.  
  9.    public worker(Socket s) throws IOException
  10.     {
  11.     InputStream in = s.getInputStream();
  12.     datain = new DataInputStream(in);
  13.     }    
  14.  
  15.    public void run()
  16.     {
  17.     try{
  18.     while(true)
  19.         {
  20.         String st = new String(datain.readUTF());
  21.         System.out.println(st);
  22.         }
  23.     }
  24.     catch(IOException ioe)
  25.     {
  26.         System.out.println(ioe);
  27.     }
  28.     }        
  29.  } 
  30.  
and I have three clients, client1.java, client2.java and client3.java

they are...
Expand|Select|Wrap|Line Numbers
  1. /*---------- client1.java ----------------*/
  2. import java.io.* ;
  3. import java.net.* ;
  4.  
  5. public class client1
  6.  {
  7.     public static void main(String args[]) throws IOException
  8.      {
  9.     Socket s = new Socket("localhost", 1234);
  10.     OutputStream out = s.getOutputStream();
  11.     DataOutputStream dataout = new DataOutputStream(out);
  12.     try{
  13.     while(true)
  14.     {
  15.         Thread.sleep(1000);
  16.         dataout.writeUTF("client1");
  17.         System.out.println("client1");
  18.     }
  19. }
  20. catch(InterruptedException ie)
  21. {
  22.     System.out.println(ie);
  23. }    
  24. //dataout.close();
  25.     //out.close();
  26.     //s.close();    
  27.      }  
  28.  }
  29.  
  30. /*----------- client2.java-------------- */
  31. import java.io.* ;
  32. import java.net.* ;
  33.  
  34. public class client2
  35.  {
  36.     public static void main(String args[]) throws IOException
  37.      {
  38.     Socket s = new Socket("localhost", 1234);
  39.     OutputStream out = s.getOutputStream();
  40.     DataOutputStream dataout = new DataOutputStream(out);
  41.     while(true)
  42.         {
  43.         dataout.writeUTF("client2");
  44.         System.out.println("client2");
  45.         }
  46.     //dataout.close();
  47.     //out.close();
  48.     //s.close();    
  49.      }  
  50.  }
  51.  
  52. /*--------------- client3.java ------------*/
  53. import java.io.* ;
  54. import java.net.* ;
  55.  
  56. public class client3
  57.  {
  58.     public static void main(String args[]) throws IOException
  59.      {
  60.     Socket s = new Socket("localhost", 1234);
  61.     OutputStream out = s.getOutputStream();
  62.     DataOutputStream dataout = new DataOutputStream(out);
  63.     try{
  64.     while(true)
  65.     {
  66.         Thread.sleep(2000);
  67.         dataout.writeUTF("client3");
  68.         System.out.println("client3");
  69.     }
  70. }
  71. catch(InterruptedException ie)
  72. {
  73.     System.out.println(ie);
  74. }    
  75. //dataout.close();
  76.     //out.close();
  77.     //s.close();    
  78.      }  
  79.  }
  80.  
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!!!!
Mar 18 '07 #1
Share this Question
Share on Google+
3 Replies


DeMan
100+
P: 1,806
I'm not certain, but I think your client classes should be threads (that is extend Thread).
For nmore information visit the Java API (If you google "Thread Java", you should find it)
Mar 18 '07 #2

10K+
P: 13,264
Hi all,
I am in a great problem. I am trying to implement a multithreaded server using java, first look at the code...

Expand|Select|Wrap|Line Numbers
  1. /*------- Main Server (server.java)--------------*/
  2. import java.io.* ;
  3. import java.net.* ;
  4.  
  5. public class server
  6. {
  7. public static void main(String args[]) throws IOException
  8. {
  9. ServerSocket s = new ServerSocket(1234);
  10. while(true)
  11. {
  12.      Socket s1 = s.accept();
  13. worker w = new worker(s1);
  14.      w.run();
  15. //s.close();         
  16. }    
  17. }
  18.  
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...

Expand|Select|Wrap|Line Numbers
  1. /*--------- worker (worker.java) -------------*/
  2. import java.io.* ;
  3. import java.net.* ;
  4.  
  5. public class worker implements Runnable
  6. {
  7. private DataInputStream datain;        
  8.  
  9. public worker(Socket s) throws IOException
  10. {
  11.     InputStream in = s.getInputStream();
  12.     datain = new DataInputStream(in);
  13. }    
  14.  
  15. public void run()
  16. {
  17.     try{
  18.     while(true)
  19.      {
  20.         String st = new String(datain.readUTF());
  21.         System.out.println(st);
  22.      }
  23.     }
  24.     catch(IOException ioe)
  25.     {
  26.         System.out.println(ioe);
  27.     }
  28. }        
  29.  
and I have three clients, client1.java, client2.java and client3.java

they are...
Expand|Select|Wrap|Line Numbers
  1. /*---------- client1.java ----------------*/
  2. import java.io.* ;
  3. import java.net.* ;
  4.  
  5. public class client1
  6. {
  7. public static void main(String args[]) throws IOException
  8. {
  9.     Socket s = new Socket("localhost", 1234);
  10.     OutputStream out = s.getOutputStream();
  11.     DataOutputStream dataout = new DataOutputStream(out);
  12.     try{
  13.     while(true)
  14.     {
  15.         Thread.sleep(1000);
  16.         dataout.writeUTF("client1");
  17.         System.out.println("client1");
  18.     }
  19. }
  20. catch(InterruptedException ie)
  21. {
  22.     System.out.println(ie);
  23. }    
  24. //dataout.close();
  25.     //out.close();
  26.     //s.close();    
  27. }
  28.  
  29. /*----------- client2.java-------------- */
  30. import java.io.* ;
  31. import java.net.* ;
  32.  
  33. public class client2
  34. {
  35. public static void main(String args[]) throws IOException
  36. {
  37.     Socket s = new Socket("localhost", 1234);
  38.     OutputStream out = s.getOutputStream();
  39.     DataOutputStream dataout = new DataOutputStream(out);
  40.     while(true)
  41.         {
  42.         dataout.writeUTF("client2");
  43.         System.out.println("client2");
  44.         }
  45.     //dataout.close();
  46.     //out.close();
  47.     //s.close();    
  48. }
  49.  
  50. /*--------------- client3.java ------------*/
  51. import java.io.* ;
  52. import java.net.* ;
  53.  
  54. public class client3
  55. {
  56. public static void main(String args[]) throws IOException
  57. {
  58.     Socket s = new Socket("localhost", 1234);
  59.     OutputStream out = s.getOutputStream();
  60.     DataOutputStream dataout = new DataOutputStream(out);
  61.     try{
  62.     while(true)
  63.     {
  64.         Thread.sleep(2000);
  65.         dataout.writeUTF("client3");
  66.         System.out.println("client3");
  67.     }
  68. }
  69. catch(InterruptedException ie)
  70. {
  71.     System.out.println(ie);
  72. }    
  73. //dataout.close();
  74.     //out.close();
  75.     //s.close();    
  76. }
  77.  
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!!!!
Your program design is wrong!
You should have a design that allows you to create as many clients as wish. You should have only one client class and be able to create many instances of that client class (as many as you wish) You should then start these clients from one main method one after the other.
Mar 19 '07 #3

Expert 100+
P: 1,510
In the server you are calling the run() method of class worker directly and getting stuck in it until the client terminates.
try starting your thread using the start() method
Expand|Select|Wrap|Line Numbers
  1.       ServerSocket s = new ServerSocket(1234);
  2.       while(true)
  3.        {
  4.          Socket s1 = s.accept();
  5.          (new Thread(new worker(s1))).start();
  6.        }    
  7.  
the server should then handle multiple clients concurrently.
Mar 22 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.