Reputation: 4120
I have problems understanding the concept behind Boost.Asio's (using v1.49.0) boost::asio::ip::udp::socket
sockets.
First I am gonna to to explain what I want to achieve:
socket.receive
(alternatively boost::asio::read
) and socket.send
(alternatively boost::asio::write
) member functions instead of the socket.receive_from
and socket.send_to
member functions.socket.send
and socket.receive
with a boost::asio::ip::udp::socket
seems to connect the socket.A UDP socket can both be bound and connected:
socket.bind
member function.socket.connect
member function.The problem is, that even though I am able to
and to be able to send data via the socket, I can not receive data from the socket. If I don't connect the socket, I can receive data via the bound local endpoint, but I am unable to send data with the approached described.
bind
or connect
with one socket instance?I know that UDP is in fact connectionless, therefore the text uses Boost.Asio terminology. I have also read connect on "connection less" boost::asio::ip::udp::socket which seems to indicate that it is impossible what I am trying.
Upvotes: 2
Views: 2217
Reputation: 1660
You are missing one point from man page ofconnect
:
If the socket sockfd is of type SOCK_DGRAM, then addr is the address to which datagrams are sent by default, and the only address from which datagrams are received.
This means, that if you want to connect
the socket, then it will be able to receive datagrams only from remote endpoint (the connected one), i.e. peer will have to bind own socket before sending datagram to your socket waiting for data.
If you need to received data from more than one peer, you can connect udp socket to "any" address (i.e. 0.0.0.0 - udp::v4()) and some port.
Upvotes: 3