I'm trying to create a server with blocking sockets (one new thread for each new client). This thread should be able to receive commands from the client (and send back the result) and periodically send commands to the client (and request back the result).
What I've thought is creating two threads for each client, one for recv
, second for send
. However:
- it's double of the normal thread overhead.
- due to request/response design, recv I do in the first thread (to wait for client's commands) can be the request I look for in the second thread (client's result to my send) and vice versa. Making it all properly synced is probably a hell story. So now I'm thinking to do that from a single thread this way:
In a loop:
setsockopt(SO_RCVTIMEO, &small_timeout);
// set the timeout for the recv (like 1000 ms).recv();
// check for client's requests first. if returns WSAETIMEDOUT than I assume no data is requested and do nothing. if I get a normal request I handle it.if (clientbufferToSend != nullptr) send(clientbufferToSend);
// now when client's request has been processed we check the command list we have to send to the client. if there is commands in queue, we send them. SO_SNDTIMEO timeout can be set to a large value so we don't deadlock if client looses connection.setsockopt(SO_RCVTIMEO, &large_timeout);
// set the timeout for the recv (as large as SO_SNDTIMEO, just to not deadlock if anything).recv();
// now we wait the response from the client.
Is this the legal way to do what I want? Or are there better alternatives (preferrably with blocking sockets and threads)?
P.S. Does recv()
with timeout returns WSAETIMEDOUT
only if no data is available? Can it return this error if there is the data, but recv()
wasn't fast enough to handle it all, thus returning partial data?