-
Multithread Server Woes
Hello everyone. I don't normally post on fora but I am really stuck!
THE PROBLEM.
I need to set up a server with multiple clients talking to each other. Sounds easy? Just a matter of setting up some Sockets with DataInputStream/DataOutputStream and some threads. Unfortunately I am having difficulty with this. Here are the steps I am taking to create my application:
Steps:
Get a client and a server to talk to each other. (I can do this)
Get multiple clients talking independently to a server. (I can do this)
Get multiple clients talking to each other via a server. (CAN'T GET PAST THIS STEP!)
As above, but with clients allocated turns to post in round-robin fashion.
As above, but with client input validated.
If someone could give me advice or a pointer to a resource to solve my problem I would be very grateful. For example: should I be using a Multicast rather than DataOutputStream to send messages from a server to simultaneous multiple clients?
cheers
Craig W
-
there should be any round robining.
you should ahve this:
A pair of SocketLinker classes.. both are runnable and operate in their own thread
a main thread that accepts connections and spawns pairs of SocketLinkers
an socket linkers job is simple:
Read everything from the inputstream and write it to the output stream
you have 2 clients, A and B
and these aer represented by the sockets to which they are connected, socketA and socketB
you create a pair of socket linkers like this. remember a socket linker takes an input and output stream as creation parameters then slips into a simple loop that forwards all input to the output
new SocketLinker(socketA.getInputStream(), socketB.getOutputStream();
new SocketLinker(socketB.getInputStream(), socketA.getOutputStream();
heres a simple loop for stream forwarding:
Code:
//inputstream is called is, output is os
byte[] buffer = new byte[4096];
for(int bytesRead = is.read(buffer); bytesRead != -1; bytesRead = is.read(buffer);{
os.write(buffer, 0, bytesRead);
}
thats it... now can you see how it links the 2 clients? the input stream of client A (what he sends to teh server) is linked to the output stream of client B (what he gets frpom the server), and vice versa
-
ill leave you to batter your head with this
-
shoot, i forgot to say that you make your sovket linkers implements Runnable interface, then put the linking loop in the run() method
then to start 2 new threads:
new Thread(new SocketLinker(A_IS,B_OS)).start()
new Thread(new SocketLinker(B_IS,A_OS)).start()
note you mustnt put:
new Thread(new SocketLinker(A_IS,A_OS)).start()
new Thread(new SocketLinker(B_IS,B_OS)).start()
because that simply sends A's data back to him and the same for b.. do you see?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks