0

All of my networking applications I have developed have used the blocking socket model. I'm about to start a new project and it requires that the user be able to send requests to a connected server and receive responses on the same socket in parallel without data race.

And might I add this is a multithreaded application as well (x clients to 1 server) and so I want to be able to send a request to the server simultaneously without having to wait for the previous recv/send but at the same time be able to receive a response on the same socket. I hope this makes sense.

The last choice I have is to use the HTTP model of connect/receive > request/serve > close for each request.

PS: I'm not looking for code

TheRealChx101
  • 1,468
  • 21
  • 38
  • This sounds like you could benefit from asynchronous networking calls- Maybe you should have a look at boost.asio and similar libraries. – Arne Mertz Aug 04 '14 at 14:18
  • Consider a toolkit like e.g. ZeroMQ, which even performs the connection part on its own and asynchronously. – Ulrich Eckhardt Aug 04 '14 at 14:44

1 Answers1

2

The way I do it is to have an I/O thread that is the only thread that is allowed to read from or write to the socket. That threads keeps a FIFO queue of outgoing-request-mesasges, and it writes data from (the head of that queue) to the socket whenever the socket select()'s as ready-for-write, and it reads from the socket whenever the socket select()'s as ready-for-read.

Other threads can add a message to the tail of I/O thread's outgoing-requests-queue at any time (note that you'll need to synchronize these additions with the I/O thread via a mutex or something, and also if the outgoing-requests-queue was empty before the new request was added to it, you'll need a mechanism to wake up the I/O thread so it can start sending the new request; writing a byte to a self-connected socket-pair that the I/O thread select()'s on the other end of will work for that purpose)

For the other direction: When the I/O thread has recv()'d a full message from the socket, it needs to deliver the received message to the appropriate worker thread; that too needs to be done by some thread-safe mechanism, but the implementation of that will depend on how the receiving thread is implemented.

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234