Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

send and recv on different threads

I'm developing a basic matchmaking server. The basic process goes like this:

  1. Client connects to server, reporting the client's name and the type of match they are looking for
  2. Server reports back all possible matches
  3. Client selects which match he/she would like to be paired with
  4. Server matches the two clients

The problem is that each time a client connects, I need to report to all possible matches this new client, and report all possible matches to that new connecting client (thus while unmatched a client must always be ready to recv from the server). At the same time, the client may at any point select a match, or disconnect (thus the client must always be ready to send and the server must be ready to recv)

I suspect that the real solution to this problem is to use threading, a background recv thread for the clients and the server, and a main user-interacting send thread.

So I have two questions relating to this:

  1. Is it save to send and recv with the same socket connection simultaneously on different threads?
  2. Is this even the right way to handle this?

(Tagged as C++ and obj-c because the sever is built in C++ and client is obj-c)

like image 802
Dan F Avatar asked Jul 05 '26 20:07

Dan F


1 Answers

Yes, it is safe to recv() on one thread and send() on another thread using the same socket.

Server-side, using dedicated threads will only work effectively if you have relatively few clients simultaneously connected to the server at the same time. If scalability is an issue, you need to use asynchronous I/O, or even I/O completion ports if possible, to manage multiple clients on a single thread, keeping the number of needed threads down to a minimum.

like image 133
Remy Lebeau Avatar answered Jul 07 '26 10:07

Remy Lebeau