Hi all,
I am in a great problem. I am trying to implement a multithreaded server using java, first look at the 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... -
/*--------- 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... -
/*---------- 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!!!!
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)
Hi all,
I am in a great problem. I am trying to implement a multithreaded server using java, first look at the 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... -
/*--------- 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... -
/*---------- 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.
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 -
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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.
|
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:
|
by: pixcee2000 |
last post by:
hi plz tell code in java program form?
|
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...
|
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.
| |
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.
...
|
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
|
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...
|
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
|
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,...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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...
| |