Reputation: 17732
I'm developing a basic matchmaking server. The basic process goes like this:
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:
send
and recv
with the same socket connection simultaneously on different threads?(Tagged as C++ and obj-c because the sever is built in C++ and client is obj-c)
Upvotes: 3
Views: 1449
Reputation: 597941
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.
Upvotes: 2
Reputation: 1176
It is safe in any reasonable socket library implementation. It is also the right way to do it.
Upvotes: 1