CptanPanic
CptanPanic

Reputation: 858

boost::asio UDP server, how to send without having to do a receive_from first?

In all the examples of the udp server, there is the code below. But in my case, I just want server to send, whether or not there is a client ready to receive. So would like to not have to use the receive_from call to get the endpoint. Can I do this in boost?

size_t length = sock.receive_from(
        boost::asio::buffer(data, max_length), sender_endpoint);
sock.send_to(boost::asio::buffer(data, length), sender_endpoint);

Upvotes: 0

Views: 914

Answers (1)

Steffen Ullrich
Steffen Ullrich

Reputation: 123260

The documentation of send_to actually contains an example on how to construct the endpoint address and send to it, without receiving from the endpoint first. To cite:

boost::asio::ip::udp::endpoint destination(
    boost::asio::ip::address::from_string("1.2.3.4"), 12345);
socket.send_to(boost::asio::buffer(data, size), destination);

Upvotes: 2

Related Questions