[Boost-users] Using boost::Asio for connected UDP socket

682 views
Skip to first unread message

Gheorghe Marinca

unread,
Aug 27, 2012, 3:54:32 PM8/27/12
to boost...@lists.boost.org
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,
    typename SocketAcceptorService = 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

Charles Mills

unread,
Aug 27, 2012, 10:51:26 PM8/27/12
to boost...@lists.boost.org

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

Eugene Mamin

unread,
Aug 28, 2012, 1:31:28 AM8/28/12
to boost...@lists.boost.org
>>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.

You should see following implementation of transport layer from
maidsafe-dht project:
https://github.com/maidsafe/MaidSafe-Transport/tree/master/src/maidsafe/transport

Also, this code reviewed by boost.asio author Christopher M. Kohlhoff.
_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Cliff Green

unread,
Aug 28, 2012, 6:11:20 PM8/28/12
to boost...@lists.boost.org
 
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
 
Reply all
Reply to author
Forward
0 new messages