49

I am implementing a simple server, that accepts a single connection and then uses that socket to simultaneously read and write messages from the read and write threads. What is the safe and easy way to simultaneously read and write from the same socket descriptor in c/c++ on linux? I dont need to worry about multiple threads read and writing from the same socket as there will be a single dedicated read and single dedicated write thread writing to the socket.

In the above scenario, is any kind of locking required?

Does the above scenario require non blocking socket?

Is there any opensource library, that would help in the above scenario?

Jimm
  • 8,165
  • 16
  • 69
  • 118

3 Answers3

42

In the above scenario, is any kind of locking required?

None.

Does the above scenario require non blocking socket?

The bit you're probably worried about - the read/recv and write/send threads on an established connection - do not need to be non-blocking if you're happy for those threads to sit there waiting to complete. That's normally one of the reasons you'd use threads rather than select, epoll, async operations, or io_uring - keeps the code simpler too.

If the thread accepting new clients is happy to block in the call to accept(), then you're all good there too.

Still, there's one subtle issue with TCP servers you might want to keep in the back of your mind... if your program grows to handle multiple clients and have some periodic housekeeping to do. It's natural and tempting to use a select or epoll call with a timeout to check for readability on the listening socket - which indicates a client connection attempt - then accept the connection. There's a race condition there: the client connection attempt may have dropped between select() and accept(), in which case accept() will block if the listening socket's not non-blocking, and that can prevent a timely return to the select() loop and halt the periodic on-timeout processing until another client connects.

Is there any opensource library, that would help in the above scenario?

There are hundreds of libraries for writing basic servers (and asking for 3rd party lib recommendations is off-topic on SO so I won't get into it), but ultimately what you've asked for is easily achieved atop an OS-provided BSD sockets API or the Windows bastardisation ("winsock").

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
  • 7
    +1 for mentioning the race condition and the design options to use threads vs select or poll – Jimm Oct 23 '12 at 19:16
  • 1
    @Tony D Good suggestion. From enhancement point of view, if in the future he plans to use OpenSSL then his architecture may change. He cannot do read and write simultaneously on the same SSL*. – enthusiasticgeek May 19 '14 at 12:15
  • @enthusiasticgeek: interesting - I haven't done SSL programming so didn't know about that, but definitely worth keeping in mind. Cheers. – Tony Delroy May 19 '14 at 12:35
  • "There's a race condition there: the client connection attempt may have dropped between select() and accept(), in which case accept() will block EVEN IF the listening socket's not non-blocking, and that can prevent a timely return to the select() loop and halt the periodic on-timeout processing until another client connects." --> Is this a better way to write what you have written here? – avernus Jul 26 '21 at 10:49
  • @AydinÖzcan: no - that suggestion is problematic. "even if" suggests the problem exists for blocking and non-blocking listening sockets (but one of those may be more surprising). That is not true - the problem only exists for blocking sockets. – Tony Delroy Jul 26 '21 at 13:17
  • 1
    @TonyDelroy I just realized that I read your original answer wrong, thanks for the help. – avernus Jul 27 '21 at 12:10
27

Sockets are BI-DIRECTIONAL. If you've ever actually dissected an Ethernet or Serial cable or seen the low-level hardware wiring diagram for them, you can actually SEE distinct copper wires for the "TX" (transmit) and "RX" (receive) lines. The software for sending the signals, from the device controller up to most OS APIs for a 'socket', reflects this and it is the key difference between a socket and an ordinary pipe on most systems (e.g. Linux).

To really get the most out of sockets, you need:
1) Async IO support that uses IO Completion Ports, epoll(), or some similar async callback or event system to 'wake up' whenever data comes in on the socket. This then must call your lowest-level 'ReadData' API to read the message off the socket connection.
2) A 2nd API that supports the low-level writes, a 'WriteData' (transmit) that pushes bytes onto the socket and does not depend on anything the 'ReadData' logic needs. Remember, your send and receive are independent even at the hardware level, so don't introduce locking or other synchronization at this level.
3) A pool of Socket IO threads, which blindly do any processing of data that is read from or will be written to a socket.
4) PROTOCOL CALLBACK: A callback object the socket threads have smart pointers to. It handles any PROTOCOL layer- such as parsing your data blob into a real HTTP request- that sits on top of the basic socket connection. Remember, a socket is just a data pipe between computers and data sent over it will often arrive as a series of fragments- the packets. In protocols like UDP the packets aren't even in order. The low-level 'ReadData' and 'WriteData' will callback from their threads into here, because it is where content-aware data processing actually begins.
5) Any callbacks the protocol handler itself needs. For HTTP, you package the raw request buffers into nice objects that you hand off to a real servlet, which should return a nice response object that can be serialized into an HTTP spec-compliant response.

Notice the basic pattern: You have to make the whole system fundamentally async (an 'onion of callbacks') if you wish to take full advantage of bi-directional, async IO over sockets. The only way to read and write simultaneously to the socket is with threads, so you could still synchronize between a 'writer' and 'reader' thread, but I'd only do it if the protocol or other considerations forced my hand. The good news is that you can get great performance with sockets using highly async processing, the bad is that building such a system in a robust way is a serious effort.

Zack Yezek
  • 1,408
  • 20
  • 7
  • 7
    'bidirectional' isn't sufficient. Half-duplex is still bidirectional, but not at the same time. It needs to be full duplex to satisfy the OP's requirement. TCP/IP is full-duplex as well as bidirectional. – user207421 Mar 29 '17 at 00:45
  • Thanks for specifically mentioning the separate physical lines. This helped me conceptualize how and why it is bi-directional and full-duplex. – Captain Man Jul 02 '19 at 17:41
26

You don't have to worry about it. One thread reading and one thread writing will work as you expect. Sockets are full duplex, so you can read while you write and vice-versa. You'd have to worry if you had multiple writers, but this is not the case.

mfontanini
  • 21,410
  • 4
  • 65
  • 73
  • Short answer, I like it. With "worry on multiple writers" you mean cuncurrent writing on the same channel will get things messed up, right? So with multiple writing threads, a syncronization method must be applied. – LppEdd Jul 09 '20 at 16:14