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

Discussion in 'Java' started by akmkat, Mar 18, 2007.

  1. akmkat

    akmkat New Member

    Joined:
    Mar 18, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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!!!!
     
  2. ganesh_don

    ganesh_don New Member

    Joined:
    Mar 9, 2007
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    The problem is ur calling the run() method of ur worker from ur main(). when u call ur run method directly multithreading has no use in ur program bcoz the control will return to the main() module only when the run method finishes,so ur server will not server mutiple clients....so what u have to do is call the w.start() (assume w as ur worker object) this will automatically call ur run() module in a seperate thread and the main thread remains in the same place where it was, so ur while loop will get repeatedly executed and it will server multiple clients

    change u have to make:-

    w.start() instead of w.run()


    assume w as ur woker object
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice