Client/Server
Client/Server
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 ??? )
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 ??? )
Only Human
Re:Client/Server
Obviously.Neo wrote: (Well I am confused ??? )
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.
Every good solution is obvious once you've found it.
Re:Client/Server
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
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?
Hope that makes it clearer.
I wondering how C++ classess will fit into all this?
Re:Client/Server
Nope, not really. :-\Hope that makes it clearer.
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.
Every good solution is obvious once you've found it.
-
- Member
- Posts: 1600
- Joined: Wed Oct 18, 2006 11:59 am
- Location: Vienna/Austria
- Contact:
Re:Client/Server
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)
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)
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
BlueillusionOS iso image
Re:Client/Server
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
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
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++?
???
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
By using the OS API. C++ does not have any multi-threading primitives / standard library functions. (It ain't Java.)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++?
Every good solution is obvious once you've found it.
- Colonel Kernel
- Member
- Posts: 1437
- Joined: Tue Oct 17, 2006 6:06 pm
- Location: Vancouver, BC, Canada
- Contact:
Re:Client/Server
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
www.boost.org
Top three reasons why my OS project died:
- Too much overtime at work
- Got married
- My brain got stuck in an infinite loop while trying to design the memory manager
Re:Client/Server
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.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++?
???
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
How are signals usually handled in C++ programs? Is there a lib for these?
Only Human
Re:Client/Server
<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.
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.
Every good solution is obvious once you've found it.
Re:Client/Server
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
I get an
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();
what can i do to work around this?error "conversion from `Socket' to non-scalar type 'ClientSocket' requested"
Only Human
Re:Client/Server
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.