The context

I need to design a Client-Server(-Client) network in C++ that would allow an android application to communicate to a Raspberry Pi and back

I am running everything on linux environment(s) and the project is time sensitive, but a latency of <80ms is manageable.

The problem

I am just starting to learn C++ and use the linux api. I come from a Java background and I have done this kind of project before in a way that I was told by my teacher is too complicated for C/C++ and it could be done easier.

The way I was doing this in Java involves BlockingQueue and a lot of threads. Let me explain:

The old way
Assume we have two clients and one server. One of the clients is a device that has sensors and the other client is a smartphone. Let's call them User (for the smartphone) and Probe (for the device).

The server can have many clients. It is always listening for new connections. Let's say we power up Probe and start the connection on User.

Now the server has two connected clients. They are identified as User and Probe by parameters in their class objects. Each of them is aware of the list of clients and each of them has two blocking queues. One is for sending data and one is for receiving data. These queues are the resources for two threads (for each client) that are the consumers.

Sending data from User to Probe follows this path:

The #receiver_thread of User is always listening for new TCP messages. Once a message has arrived, it is immediately processed. The messages can be of many forms (exit, bind_to_probe, send_message_to_probe etc)

If the message is send_message_to_probe, checks are made, the probe is chosen and a new message is created. Finally, the message is added to the sending_blocking_queue of the Probe.

The #sending_thread of a client on the server is very simple: it takes the sending_blocking_queue and it picks the oldest message. This is packed and sent to the Probe device.

The C way? The reason I am writing this question/post is to ask if there is a more efficient way to doing this. Java abstracts all the hardware, pointers, memory and drivers from the users and I am really eager to learn how to do it in C/C++ properly.

A teacher recommended me to use linux's polling, select and file descriptors. I tried doing some reading on the linux manual, but it did not quite make sense to me on how that could help in my scenario.

Sorry for the long description, but I wanted to put you in context. Big thank you if you got to the end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.