C - sockets - epoll. What about slow clients?

Context

Debian64bits.

Thought I understood the consequences of the socket, but didn't.

Worried about the management of slow clients.

Read and reproduce this code generated click epoll

Imagine two clients: A: very slow network B: very fast network.

Question

In edge-triggered mode, what happens when we start reading from A and then B also sends some data to read while we are in a read loop for A?

Will we wait until we read all the data coming from A?

Or should we create a buffer (not in the example shown) to hold A's data and concatenate them until the message is complete and returns immediately?

Or is this buffer already managed by epoll?

The reason is not clear to me.

+3


source to share


1 answer


It doesn't matter if the network is slow, the reading part stops whenever it reads all the data that has reached the server.

It has a bug (or at least a portability issue). When reading data, the code only checks EAGAIN

. It should also check EWOULDBLOCK

as some platforms return this error code when all data has been read.

Edit:

Ok, so if I want to put the incoming data into the database, how can I manage it? Will I be able to read all the data?



If you receive an incomplete message, you will need to save it until you have received the entire message. I'd malloc

a char*

for each incoming connection with a message fragment.

When the message is complete, you can process it, save it to the database, or whatever.

Note that the beginning of the next message may already be available, so you cannot stop reading until you receive EWOULDBLOCK

or EAGAIN

.

Another way to solve the problem is to create a separate thread for each connection. Which is the best option? Well it will depend on the application (number of concurrent users, etc.).

+3


source







All Articles