Page 1 of 2
Client/Server
Posted: Mon Nov 22, 2004 5:59 am
by Neo
I have a client server scenario to implement (first time doing network sockets
). and there are situations where i have to receive as well as send messages.
My problem is that i thought of having one program to receive the messages and decode them and then send the results to the original sender after sending another message to the next server.
Obviously this is not very nice so does anyone have any pointers on how actually this is implemented in the real world. Do you use separate clients and servers for sending and recieving or then what else could you do. (Well I am confused ??? )
Re:Client/Server
Posted: Mon Nov 22, 2004 7:00 am
by Solar
Neo wrote:
(Well I am confused ??? )
Obviously.
I'm not really sure what your problem is. A server sets up a port, and waits for a connection. Once the connection is established by the client, you have a two-way connection anyway...?
I'm confused, too. Please elaborate.
Re:Client/Server
Posted: Tue Nov 23, 2004 5:37 am
by chris
Your logic above doesn't really have any flaw, but if you want better ideas we need to know why you're sending what, to where.
Re:Client/Server
Posted: Tue Nov 23, 2004 6:12 am
by Neo(out)
The scenario is a simulation of the variuos node in a cellular phone network. One node has to get message process (if necessary) and then send a message to the next node and waitfor a reply. this should enable request to come even while processing the previous ones.
Hope that makes it clearer.
I wondering how C++ classess will fit into all this?
Re:Client/Server
Posted: Tue Nov 23, 2004 8:47 am
by Solar
Hope that makes it clearer.
Nope, not really. :-\
Make a drawing of the message flow. UML can help here but isn't a neccessity. Show us how the messages are supposed to happen, on a time string. Who messages whom, in which order, including error / timeout handling.
Once you have that, chances are you found your answer already. If you didn't, it will help
us to find it.
Re:Client/Server
Posted: Tue Nov 23, 2004 9:13 am
by distantvoices
Neo, you know how to sketch a protocol? make a ( i don't know the correct naming of it anymore but am using it a lot in brains, when thinking about how to implement several things) condition-event-grid:
in vertical you have several conditions which can take place during the execution of your system.
in horizontal, you have a set of certain events which can occur. Then you assign each event/condition combo which might be possible, an action with parameters. This can even include a resend or reset of state.
That's how it looks like when I am doing such kind of stuff.
Hope this rough sketch helps. I gonna look up the correct naming of that thing. maybe they tell it you at school/course?
stay safe.
Oh and btw: if you are doing sockets and have a socket open - if the other part has sent data, it will be there to be fetched by any socketRead call (which is not necessarily blocking, so you can set off doing useful things until data arives). You can send occasional ack messages back using the same socket. after allthe socket represents a logical connection between two partners which is bidirectional as solar has stated. so use this feature to your prospering. (sic, what an english)
if you re doing java: use threads. they arent hard to grasp. I use them in my own os too to quite an extent (gui listeners, file service fs-driver threads)
Re:Client/Server
Posted: Tue Nov 23, 2004 10:16 am
by Candy
I consider this to be the basic class model for any language using classes to implement an asynchronous c/s model:
You have a class thread, from which three classes of threads inherit.
The first is the receiver-thread, which receives a command (from the socket), translates it into an internal state object equivalent to the command and stores it in the in-fifo queue.
The second is the sender-thread, which takes responses from the internal out-fifo and sends them to the applicable socket. It only does this. You can leave this one out, but it looks nicer (imo) if it's separated.
The third, and most important, is the worker thread. This one knows nothing of C/S or protocols or anything (hence the separation with the sender) and only receives a single command on the input-fifo to process onto the output-fifo. It does this in its own time.
A CS application consists of one (or more) reader threads, preferably sqr(n) readers for n sockets (so, each reader has x threads, and there are x readers). This kind of optimizes the speed the readers work at versus the select() system call. It has any random number of worker threads that suffices, but at least 1 for each processor of the machine. If it does mostly blocking operations (webserver, something like that) it needs more than one per processor. You can make it a parameter of the program. It needs some senders, but I have no numbers on how many. My guess is about 2-3 for most loads, since they work without delays.
Each has a pointer/reference to the main class of the program (of whatever name) so they can all use that one. It controls the sockets (also a class), and makes sure they are reconnected etc..
Each socket inherits from a base socket (which may or may not be standard for your language), and extends it by either a nicer interface or additional functions (such as encryption, compression etc.).
This pretty much describes your basic C/S program. If you're not doing this for your own enjoyment or some decent reason (but rather a school project etc.) make a single program that launches a socket-bound thread that processes a single operation at a time. It's slow and heavy on your server, but who cares? It's easiest to program
Re:Client/Server
Posted: Wed Nov 24, 2004 6:50 am
by Neo(Out)
This is slightly deviating from the real thing but i need to know this.
How do we bring in the concept of multi-threading in C++ . i.e i want to fork off a thread on each connect() how do i do this in C++?
???
Re:Client/Server
Posted: Wed Nov 24, 2004 7:37 am
by Solar
Neo(Out) wrote:
How do we bring in the concept of multi-threading in C++ . i.e i want to fork off a thread on each connect() how do i do this in C++?
By using the OS API. C++ does not have any multi-threading primitives / standard library functions. (It ain't Java.)
Re:Client/Server
Posted: Wed Nov 24, 2004 9:50 am
by Colonel Kernel
Boost has a threading library that is relatively easy to use. It also has a lot of other neat stuff that is worth a look:
www.boost.org
Re:Client/Server
Posted: Wed Nov 24, 2004 11:25 am
by Candy
Neo(Out) wrote:
This is slightly deviating from the real thing but i need to know this.
How do we bring in the concept of multi-threading in C++ . i.e i want to fork off a thread on each connect() how do i do this in C++?
???
You create a new thread with pthreads (assuming linux/unix with pthreads). You do that by calling pthread_create with a struct containing an object pointer and pointing it to a static member function of your class (or a normal c function) that starts the function you want started.
Since this is all pretty much the same for all threads, make a default thread-class of it and derive
... Know that multiple inheritance is one of C++'s advantages, and use it to yours.
Re:Client/Server
Posted: Fri Nov 26, 2004 6:04 am
by Neo
How are signals usually handled in C++ programs? Is there a lib for these?
Re:Client/Server
Posted: Fri Nov 26, 2004 7:36 am
by Solar
<csignal> has the familiar C functions. Again, your OS should provide auxiliary, system-specific functionality.
The reason why the C++ standard library doesn't have most of these things is because there is no standard way to do multithreading, signals et al. They are the domain of the operating system, and as such C++ doesn't intrude on them.
Re:Client/Server
Posted: Sun Nov 28, 2004 3:45 am
by Neo
I have a problem in C++. Right now i have created a 'Socket' class as the base and 2 derived classes named 'ClientSocket' and 'ServerSocket' which implement functions necessary for their respective needs only.
The accept() function wrapper in my 'ServerSocket' class returns a object of type 'Socket' (the base class type). Unfortunately when i need to create a 'ClientSocket' by specifying somethin like this
Code: Select all
ServerSocket ss;
ClientSocket cs=ss.Accept();
I get an
error "conversion from `Socket' to non-scalar type 'ClientSocket' requested"
what can i do to work around this?
Re:Client/Server
Posted: Sun Nov 28, 2004 4:37 am
by Candy
Either make your ServerSocket return a ClientSocket (instead of a Socket) explicitly, or dynamic_cast<ClientSocket>(object) it to the right type. If neither works, you are trying a really invalid assignment. Don't do that.