views:

347

answers:

2

Hi all!

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.

A: 

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.

Preet Sangha
A: 

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).

Nikolai N Fetissov