Original Post
Hello everyone. I have a question about the usage of UDP sockets in an online game. I currently have the design where client has a non-blocking socket assigned to it. I know, you don't need a socket-per-client, as you can always use recvfrom(), but I used connect() to bind the socket to a IP/port combination so that all send() calls went to the correct address, and all recv() calls would receive data only from that IP/port combination. I have multiple threads, usually three or four that handle all of the clients, and they load balance themselves. Each thread does a recv() on its respective clients' sockets, and processes the data. And right now, it works just fine. However, I had heard that this was a *very* poor architecture, and that using a single UDP socket for the entire server was the best plan, and have recvfrom() return the IP/port, then find the associated client, perhaps then shift the load to a different thread to utilize > 1 CPU. I honestly can't understand why the first wouldn't be better than the second. It seems to be that being able to use multiple processors on the server and not have to sort through messages from other users (because recv() after connect() filters UDP traffic so that only UDP traffic from that IP/port can be read()) would be a win. Moreover, there is no synchronization required between the threads in the first to merely recv() a packet, which should improve scalability, since otherwise, each packet would need to be synchronized with another thread. The only thing I think my architecture (the first one) would not scale at is the large number of system calls (i.e. recv() being 99% of them). But... Couldn't you use epoll() (Linux) or /dev/poll (Solaris) to pack all of a worker thread's information into a single test for data, THEN recv() if needed to reduce the number of system calls? (Non-blocking sockets would be pretty useless then, but that can be turned on/off easily with a single line of code.) Anyways, if there is something glaringly obvious that I am missing, please help me! I would appreciate as detailed an explaination as possible. If someone has a better architecture that they think might work, then feel free to explain it too!