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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • akmkat
    New Member
    • Mar 2007
    • 6

    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!!!!
  • DeMan
    Top Contributor
    • Nov 2006
    • 1799

    #2
    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)

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by akmkat
      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!!!!
      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.

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        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
        Code:
              ServerSocket s = new ServerSocket(1234);
              while(true)
               {
                 Socket s1 = s.accept();
                 (new Thread(new worker(s1))).start();
               }
        the server should then handle multiple clients concurrently.

        Comment

        Working...