473,378 Members | 1,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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

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
3 2627
DeMan
1,806 1GB
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
r035198x
13,262 8TB
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
horace1
1,510 Expert 1GB
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

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

Similar topics

2
by: Jim G. | last post by:
I'm looking to build a java application like you see on sports websites, where the information is updated live, for instance in baseball the balls, strikes, etc. are updated. Any good points of...
0
by: news.onet.pl | last post by:
I hava a problem with communication between Java/Corba server based on JDK ORB with Java/Corba client (applet) based on the same ORB. I`m using IOR to localize server. client`s ORB i initialize...
1
by: pixcee2000 | last post by:
hi plz tell code in java program form?
2
by: shblack | last post by:
Please can someone help me with this program. I am in a JAVA programming class and I am having a heck of a time. I am still having a problem with the basic concepts of JAVA but the teacher continues...
9
by: Andreas Schmitt | last post by:
I am workin on a 2 part project right now. The first part is a DLL, the second part a normal exe using that DLL. When I use the VS2005 standard setting for compiling with the Multithreaded-DLL...
0
by: arunbalait | last post by:
How can i send data from my web page to a sql server that doesn't reside in the same server as my web page. How can i set up my data base in a way i can connect to it via internet. Am going to use...
1
by: sasha80 | last post by:
Hello everybody here is Sasha from russia ... I'm working as .Net 2005 application developer I prepare to gain MCPD certificate i have to pass three exams 70536(first) 70526(second)...
1
by: MeGeraldo | last post by:
I'm working on a project for a work agency in the Netherlands. I made a java application for the workers to see what time they work at which company and which car they have to take to get there. ...
1
by: sanjana Bhatt | last post by:
i want to implement a multithreaded server having atleast 3 client threads.. i m quite new to server programming.. can u guys suggest me how to proceed plz help.. its urgnt
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.