How to Waiting simultaneously for client connections and Centralserver outputs

A

ahd292

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?
 
M

Mark Space

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.

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/
 
A

Armando

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.
 
A

Armando

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
 
M

Mark Space

Armando said:
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/
 
M

Mark Space

Armando said:
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.
 
L

Lew

... 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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top