Using boost::Asio for connected UDP socket
Hi, I want to implement an UDP server and have problems on defining on how the server handles coming concurrent udp "connections" from multiple clients. What I basically want to implement is an DNS proxy. Is there something equivalent to an tcp::acceptor for udp ? Looking over asio docs I see: template< typename Protocol http://www.boost.org/doc/libs/1_35_0/doc/html/boost_asio/reference/Protocol...., typename SocketAcceptorService http://www.boost.org/doc/libs/1_35_0/doc/html/boost_asio/reference/SocketAcc... = socket_acceptor_service<Protocol>>class basic_socket_acceptor : public basic_io_object< SocketAcceptorService >, public socket_base For tcp we have following typedef: typedef basic_socket_acceptor< tcp > acceptor; Can I use basic_socket_acceptor with socket_acceptor_service<UDP> so that I can create additional udp sockets for every udp client that wants to send data to my server ? I've observed that if I use async_receive_from on the same socket I receive data from multiple clients making impossible exchanging data (read/write) with multiple clients at the same time. Any hints are appreciated. -Ghita
I know nothing about asio but UDP is itself inherently connectionless. You don't need multiple sockets or threads or the like to handle multiple clients. Think of your mailbox (the old-fashioned one that gets paper mail): no matter how many people send you letters, you have and need only one mailbox. All the letters arrive there, in no particular order, and with no relationship between one letter from your friend Joe and the next letter from Joe. You can receive correspondence simultaneously from any number of people with one mailbox. TCP is more like a telephone: if you want to talk to ten people at once you need ten telephones (okay, yes, I know about conference calls but you get what I am saying). If you want to keep track of a "conversation" with individual senders then you will need some sort of structure and logic inside your program to do so. Again, I know nothing about asio but with native sockets you do get told the "return address" (the sender's IP address) for each incoming "datagram" as UDP messages are called. One nice thing about UDP: if the sender sends a message ABC and then another message DEF you will get two messages, one ABC and one DEF; unlike TCP, where in theory you might get one message of ABCDEF or even one of AB and one CDEF. (In UDP the two messages *might* arrive out of order, DEF and then ABC; or conceivably not arrive at all.) Also, unlike TCP, the sender does not receive any kind of an error for undeliverable messages. I can write a program that sends UDP messages to random IP addresses and I will never receive any error indication whatsoever. Hope this helps, Charles From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Gheorghe Marinca Sent: Monday, August 27, 2012 3:55 PM To: boost-users@lists.boost.org Subject: [Boost-users] Using boost::Asio for connected UDP socket Hi, I want to implement an UDP server and have problems on defining on how the server handles coming concurrent udp "connections" from multiple clients. What I basically want to implement is an DNS proxy. Is there something equivalent to an tcp::acceptor for udp ? Looking over asio docs I see: template< typename Protocol <http://www.boost.org/doc/libs/1_35_0/doc/html/boost_asio/reference/Protocol .html> , typename SocketAcceptorService <http://www.boost.org/doc/libs/1_35_0/doc/html/boost_asio/reference/SocketAc ceptorService.html> = socket_acceptor_service<Protocol>> class basic_socket_acceptor : public basic_io_object< SocketAcceptorService >, public socket_base For tcp we have following typedef: typedef basic_socket_acceptor< tcp > acceptor; Can I use basic_socket_acceptor with socket_acceptor_service<UDP> so that I can create additional udp sockets for every udp client that wants to send data to my server ? I've observed that if I use async_receive_from on the same socket I receive data from multiple clients making impossible exchanging data (read/write) with multiple clients at the same time. Any hints are appreciated. -Ghita
To clarify, in case I misunderstood the various questions / replies: Create one UDP socket (bound to a port) for incoming datagrams. From then on, all incoming datagrams (that are sent to that address / port) can be “replied” to with “receive_from” and “send_to” – no additional sockets need to be created. This is a common use case for datagrams. Cliff
participants (3)
-
Charles Mills
-
Cliff Green
-
Gheorghe Marinca