How to Waiting simultaneously for client connections and Centralserver outputs

Discussion in 'Java' started by ahd292, Apr 16, 2009.

  1. ahd292

    ahd292 Guest

    1) Hi everyone. I am working on a network based project that
    implements "Multi Client–Multi Server" scenario. In our design we must
    create one Central Server and some Secondary server that respond to
    client request. At first C.S runs and then some S.S. Secondary servers
    connect (TCP) directly to Central Server. In fact Secondary servers
    are clients of C.S. I have created 3 classes for first section:

    1) CentralServer 2) Secondary 3)SSHandler

    For multithreading I create third class as a worker for handling
    communication between C.S and S.S.
    In second Section Secondary servers are responsible for processing
    client requests. Now they (SS) are act as Server. Each client connect
    to one of these S.S (by using of an special algorithm). Like to first
    section I have 3 classes for this section:

    1)Secondary(this is same class as above) 2)Client 3)ClientHandler

    For multithreading I create third class as a worker for handling
    communication between Client and S.S.
    Now my problem occurs when in Secondary class I have to waiting
    simultaneously for client requests for connection and data in way from
    C.S socket. This is some of my code of Secondary

    public class Secondary {
    public Secondary() {
    csSocket = new Socket(IP, 1234);
    output = new ObjectOutputStream(csSocket.getOutputStream
    ());
    in = new ObjectInputStream(csSocket.getInputStream());
    ServerSocket ssSocket = new ServerSocket(port);
    while (counter < MAX_CLIENT) {
    System.out.println("ss waiting for connection");
    Socket connection = ssSocket.accept();
    ClientHandler handler = new ClientHandler(this,
    connection, "id of client");
    handler.start();
    clients.add(handler);
    counter++;
    }
    public static void main(String[] args) {
    Secondary ss = new Secondary();
    }

    At this moment I just handle connections from clients and I can't know
    how to wait for reading objects received from CS. for example suppose
    that C.S write something in its output socket and S.S must read it but
    in other hand it must wait in while(true) loop for receive client
    connections.
    Do I create thread for these two works or another solution?

    2) and another question. In my secondaryHandler class I create a
    object that is instance of one of my date classes named Book. Each
    book has a list of another objects that they are instance of Section
    Class. Both of those classes are Serializable More in code:

    Book book = new Book("name");
    Section s = new Section("name");
    book.add(s);;
    output.writeobject(book)

    now when I print sections of book for test it works and book has
    section.
    but in client class when I read and cast object to Book there is no
    section in it. Why?
     
    ahd292, Apr 16, 2009
    #1
    1. Advertising

  2. ahd292

    Mark Space Guest

    Peter's advice was pretty good. Multiple threads or NIO is likely the
    answer. However, it seems to me that you are getting to the stage of
    your programming where your program is complex enough that you can't
    just hack your way through it anymore. You'll need to carefully design
    the software so that it can and will work, regardless of which way you
    choose to go (threads or NIO). It won't just happen on its own by
    throwing some Java package at it.

    Start with some UML diagrams or whatever form of software engineering
    you're familiar with. Keep at that until you think you've got it
    nailed. Then start implementing parts of the design along with unit
    tests until you can either see the parts of the design that must be
    changed, or it all starts working the way you intended.


    ahd292 wrote:

    > now when I print sections of book for test it works and book has
    > section.
    > but in client class when I read and cast object to Book there is no
    > section in it. Why?



    I agree with Peter. It should work. So it must be some coding error.
    Please duplicate the error with an SSCCE and post the SSCCE here so we
    can take a look at it.

    http://sscce.org/
     
    Mark Space, Apr 16, 2009
    #2
    1. Advertising

  3. ahd292

    Armando Guest

    > In Java, there are two main strategies for dealing with that.  Create a  
    > new thread to handle each connection, or use the classes in java.nio,  
    > giving you a "select" API for interacting with the sockets.
    >


    thanks Peter. It seems my problems was not clear for you or maybe I
    can't explain it clearly. as you say threads are one way to dealing
    with that. and I used it by create one thread for each connection from
    client. as you see in while loop when SS accept connection from
    client, pass its allocated socket to ClientHandler that Extends Thread
    and then start it. My problem is not here. I said that during waiting
    for connections from clients, SS must also wait for data that comes
    from Central Server NOT Client. Notice that SS is Server for clients
    and Client for Central Server. these two jobs must be done together
    without blocking another. I know that it needs synchronization but I
    want some simple code specially for my problem in networking to solve
    it.

    now if nio can help me please explain more about it. does "select" API
    solve this problem.

    finally excuse me for sending 2 question in one post. although both
    are in networking topic.
     
    Armando, Apr 16, 2009
    #3
  4. ahd292

    Armando Guest

    > Start with some UML diagrams or whatever form of software engineering
    > you're familiar with.  Keep at that until you think you've got it
    > nailed.  Then start implementing parts of the design along with unit
    > tests until you can either see the parts of the design that must be
    > changed, or it all starts working the way you intended.


    problem statement:
    We have one Central Server that control all information and updates
    all Secondary Servers and clients. after that Central Server runs it
    is time to Start Some Secondary Server. SS first find CS and set up
    TCP connection to it. we can have any number of SS connected to CS.
    then when client starts find one SS and connect to it. now client is
    connected to CS by using of SS. like SS we can have any number of
    clients connected to SS. each SS can accept many clients and each
    client can connect only to one SS.
    now is my design wrong? is there any better design for it? I wait for
    your solution. keep in mind that SS is Server and at the same time is
    Client. Do I split SS class in two class: SServer and Sclient? at this
    time I didn't have any idea but if you have I will be happy to know
    your idea.
    thanks
     
    Armando, Apr 16, 2009
    #4
  5. ahd292

    Mark Space Guest

    Armando wrote:

    > now if nio can help me please explain more about it. does "select" API
    > solve this problem.



    Yes. select() allows you to wait on multiple IO channels at the same
    time, and then when one becomes ready it will "select" that channel and
    return it to you. Thus, you would wait on both your client and server
    channel, and which ever one had data available would be returned to you
    for processing.

    Do a search on "java nio tutorial" there's a lot of stuff available.

    This one here looks pretty decent:

    http://rox-xmlrpc.sourceforge.net/niotut/
     
    Mark Space, Apr 16, 2009
    #5
  6. ahd292

    Mark Space Guest

    Armando wrote:

    > now is my design wrong? is there any better design for it?


    The issue is that a problem statement isn't a design.

    It's also incomplete. What do you do after you connect anyway? What
    kind messages do you process? What about disconnects? User
    authentication? Error logging? A zillion other possibilities.

    > I wait for
    > your solution.



    Not likely. Do your own damn homework.


    > keep in mind that SS is Server and at the same time is
    > Client. Do I split SS class in two class: SServer and Sclient? at this
    > time I didn't have any idea but if you have I will be happy to know
    > your idea.



    This is implementation. Design first. I'd like to see some use cases.
    The start a class and package breakdown. Since you are into multiple
    threads here, you'll have to detail how the threads should interact. At
    that point I think you'll have something closer to an implementable design.

    So far for a use case, all I see is this


    Client -- connects to server

    Server -- accepts client connection

    What the heck happens next is totally opaque to me.
     
    Mark Space, Apr 16, 2009
    #6
  7. ahd292

    Lew Guest

    On Apr 16, 2:23 pm, Armando <> wrote:
    > ... I used it by create one thread for each connection from
    > client. as you see in while loop when SS accept connection from
    > client, pass its allocated socket to ClientHandler that Extends [sic] Thread
    > and then start it. My problem is not here. I said that during waiting
    > for connections from clients, SS must also wait for data that comes
    > from Central Server NOT Client. Notice that SS is Server for clients
    > and Client for Central Server. these two jobs must be done together
    > without blocking another. I know that it needs synchronization but I
    > want some simple code specially for my problem in networking to solve
    > it.
    >


    What needs to be synchronized?

    Waiting on a socket doesn't. You handle requests serially. By
    passing off the connection to a service thread, the listening thread
    can get right back to listening for the next request.

    It doesn't, or shouldn't, matter to the listening thread whether the
    request comes from a client or a "SS" server.

    What needs synchronization is access (both read and write) to data or
    other resources shared by the service threads.

    Whether your "SS" servers need multiple threads in turn depends on the
    service model. If they act as strict relay stations - wait for and
    receive client request, send to server, wait for and receive server
    reply, reply to client - with no interleaving of requests and replies,
    then you can it all in one "SS" server thread.

    --
    Lew
     
    Lew, Apr 16, 2009
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Olivier Matrot
    Replies:
    1
    Views:
    387
    bruce barker
    Sep 12, 2007
  2. jobs
    Replies:
    2
    Views:
    918
  3. binaryj
    Replies:
    1
    Views:
    517
    Jonathan Gardner
    Mar 24, 2008
  4. ids
    Replies:
    4
    Views:
    1,567
    Keith Thompson
    Apr 14, 2009
  5. Replies:
    4
    Views:
    161
Loading...

Share This Page