473,763 Members | 6,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

6 New Member
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 2642
DeMan
1,806 Top Contributor
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 MVP
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 Recognized Expert Top Contributor
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
1631
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 reference on the net for something like this? Java is very new to me so I would need something pretty basic. Thanks.
0
3272
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 like that: Dane proxy = null; ORB orb = ORB.init(parent, null); org.omg.CORBA.Object obj = orb.string_to_object(sIOR); proxy = DaneHelper.narrow(obj); server`s ORB i initialize like that:
1
1465
by: pixcee2000 | last post by:
hi plz tell code in java program form?
2
1606
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 to give us difficult assignments with out any help. Here is the assignment: Write a class named 'Player' that has two attributes, 'name' (which has to be a 'Sting') and 'rating' (which has to be a 'int'). Then write an application class named...
9
3805
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 runtime library (compiler option /MD )everything works fine on my PC. But when I try to run the thing on a friends PC or my laptop I get: "This application has failed to start because the application configuration is incorrect.
0
852
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 my database from Internet. so that my webapplication can run anywhere. So plz help me how to connect with SQL Server through internet in asp.net. Or do I have to post this question in the SQL Server forum. I am confused where to post it. ...
1
1274
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) 70548(third) I need your help ... plz
1
1294
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. I want to retrieve this information from a MSSQL database into my java app into jtabels. The only problem i have is the MSSQL query to accomplish this. These are the tables in my mssql database on a ms sql server: tbl.Employee Employee_ID PK...
1
2437
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
0
9386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10144
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9997
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9822
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8821
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7366
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.