3

I have a client/server program written in C++. I want to check the client response (an attribute of a C++ object) through a command send by the server, with a timeout if no response.

I am waiting for an expected value during some seconds. If the expected value is not observed, I need to return with a timeout. I was thinking about a thread and a poll to check the expected value in an specific time interval.

I wonder if C++11/14 features - std::promise, std::future, std::condition_variable or something else - can do it more easily for this case. The inconvenient i see about it is that i have to notice each changing value with a notify.

Well, i need some advice.

2
  • 1
    What networking library are you using? That should already have this functionality built in, without the need to do this manually. Commented Nov 13, 2019 at 13:41
  • Show your code, please. Commented Nov 13, 2019 at 14:00

5 Answers 5

2

None of the C++ language features you mentioned can help in your scenario, because they are intended for interaction within a single running program - which may be multi-threaded, but not separated into two completely independent processes.

However, the networking library you are using (on the server side) might possibly have convenience facilities for doing this.

I realize this is a general and somewhat vague answer, but your question was also not very specific.

Sign up to request clarification or add additional context in comments.

4 Comments

For example, i have to check the number of exchanged messages through a request (a simple int member in my test server) or a value in the client response. The test has to validate the response (corrected value) or not (wrong value or timeout). I think a personal polling was a bit heavy compare to the simplificity i found in these features.
@YopAndMail: Communications among different programs, possibly on different machines, will necessarily be more complex using a programming language not intended to write multiple independent programs at once. Note, though, that in many cases you don't have to repeatedly-poll, but go to sleep until a message is available from the client or some time passes.
In fact, just talking to all of you makes me take a step back. I think i searched too much complexity (my problems mixes architecture and implementation in my head). The timeout will be, obviously, implemented by my test server and my caller tests, will (in)validate the response the server gets. Well, it is so simple as I'm a little ashamed now..
@YopAndMail: There is nothing to be ashamed about. You asked a legitimate - though somewhat inspecific - question and got an answer. Everything's ok.
0

How to wait for a value with timeout

Within a process, one would typically use a condition variable.

I want to check the client response ... through a command send by the server

There is no standard way to communicate between processes in C++ (unless you count interaction with filesystem). As such, there is also no standard way to enforce a timeout on such communication.

Before you can know how to implement the timeout, you must figure out how you are going to communicate between the client and the server. That choice will be affected by what system you are targeting, so you should first figure that out.

Comments

0

If you are on a Linux environment you can try rpcgen and play with .x flies but you’ll have to study it a bit. Not sure for Windows env. Also you can use Dbus which is more intuitive. [edit] Dbus or probably libdbus for you is an IPC cross platform toolkit or library that can fit your need. RPCGEN is an old tool that does the same thing but more complicated. I don’t have a snippet, I apologize but you can search for “qt dbus example”.

3 Comments

Please provide a little more information than just name-dropping... what is rpcgen? What are .x files? What is D-Bus? Maybe some of us know the answer, but not all of us. You can also add links to further information.
Dbus or probably libdbus for you is an IPC cross platform toolkit or library that can fit your need. RPCGEN is an old tool that does the same thing but more complicated. I don’t have a snippet, I apologise but you can search for “qt dbus example”
I meant, put that in the answer please... :-P
0

About the first requirement, server waits for a response with a timeout.
Have you tried select() or poll(). They can help us to monitor the socket connection between server and client in a period.
Or we can use signal() and alarm(), to check the response after a few seconds.
In Bekerley API, combine setsockopt() with SO_RCVTIMEO, SO_SNDTIMEO can also set the timeout for the request.
I'm not sure about the library you are implementing, but I hope it has any similar functions.

The second requirement, you are waiting for expected value for a duration.
I think condition variable is a good solution for this.

Comments

0

Why not using boost::thread with a timed_join?

 boost::thread server_thread(::server_checker_method, arg1, arg2, arg3); if (server_thread.timed_join(boost::posix_time::milliseconds(1000))) // wait for 1s { // Expected value found in the server in less than 1s } else { // Checking expected value took more than 1s, timeout !!! } 

You can put your checking mechanism in the server_checker_method and return if the expected values are OK. Otherwise, iterate over the loop until the timeout reaches.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.