Multithread Server Woes


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Multithread Server Woes

  1. #1
    Join Date
    Mar 2004
    Posts
    2

    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

  2. #2
    Join Date
    Feb 2004
    Posts
    808
    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
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  3. #3
    Join Date
    Feb 2004
    Posts
    808
    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?
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center