1
votes

This is a simple problem, but I can't seem to figure out what I am doing wrong. I am attempting to read data sent to a port on a client using Boost and I have the following code which sets up 1) the UDP client, 2) a buffer for reading to and 3) an attempt to read from the socket:

// Set up the socket to read UDP packets on port 10114
boost::asio::io_service io_service;
udp::endpoint endpoint_(udp::v4(), 10114);
udp::socket socket(io_service, endpoint_);

// Data coming across will be 8 bytes per packet
boost::array<char, 8> recv_buf;

// Read data available from port
size_t len = socket.receive_from(
            boost::asio::buffer(recv_buf,8), endpoint_);

cout.write(recv_buf.data(), len);

The problem is that the recieve_from function never returns. The server is running on another computer and generating data continuously. I can see traffic on this port on the local computer using Wireshark. So, what am I doing wrong here?

1
There are may reasons why data wouldn't show up in the application layer. What OS? - Chad
OS is Ubuntu 16.04. - cirrusio
So, tried listening using netcat via nc -luv 10114 and I get the data I expect. - cirrusio

1 Answers

1
votes

So, it turns out that I need to listen on that port for connections coming from anywhere. As such, the endpoint needs to be setup as

boost::asio::ip::udp::endpoint endpoint_(boost::asio::ip::address::from_string("0.0.0.0"), 10114);

Using this setup, I get the data back that I expect. And fyi, 0.0.0.0 is the same as INADDR_ANY.