C++ Winsock non-blocking/async UDP socket

I'm developping a little data processor in c++ over UDP sockets, and have a thread (just one, and apart the sockets) that process the info received from them.

My problem happens when i need to receive info from multiple clients in the socket at the same time.

How could i do something like:

Socket foo;
/* init socket vars and attribs */
while (serving){
thread_processing(foo_info);
}

for multiple clients (many concurrent access) in c++?

I'm using winsocks atm on win32, but just get standard blocking udp sockets working. No gui, it's a console app. I'll appreciate so much an example or pointer to one ;).

Thanks in advance.

Answers


UDP socket is able to receive datagrams from multiple clients with the recvfrom() function. Just block on receive, read the request, process it, send the reply, repeat. You don't even need a thread unless processing takes a very long time (in that case a thread connected with two queues, in- and out-, would work).


I would suggest this is best tackled by putting the requests in a queue and letting the other thread work off the queue. This decouples the socket receive from the process and thus allows you to scale to more listeners and processing threads if your requirements change.


Need Your Help

heatmap isn't working

javascript google-fusion-tables

I followed a sample code to create a fusion tables map with 'heatmap' (hotspots as I called it) but it's not working. Could someone take a look on it, please?

uninitialized constant ActiveAdmin error when deploying on vps

ruby-on-rails ruby ruby-on-rails-3 rubygems activeadmin

I'm trying to deploy my rails app on a VPS with capistrano and I am having this issue: