Brian Yeh
Brian Yeh

Reputation: 3267

C++ udp boost client doesn't receive udp messages

When I run:

nc -ul <ip address> <port>

I receive messages from the udp server. However when I try to reproduce the equivalent using the below C++ program with boost it just freezes on receive_from:

#include <iostream>
#include <boost/array.hpp>
#include <boost/asio.hpp>

using boost::asio::ip::udp;

int main(int argc, char *argv[]) {
    try {
        if (argc != 3) {
            std::cerr << "Usage: client <host> <port>" << std::endl;
            return 1;
        }

        boost::asio::io_context ioContext;

        udp::resolver resolver(ioContext);
        udp::resolver::query query(udp::v4(), argv[1], argv[2]);
        udp::endpoint endpoint = *resolver.resolve(query);

        udp::socket socket(ioContext);
        socket.open(udp::v4());

//        boost::array<char, 1> sendBuf = {0};
//        socket.send_to(boost::asio::buffer(sendBuf), endpoint);

        char letter = 0;
        udp::endpoint sender_endpoint;
        while(socket.receive_from(boost::asio::buffer(&letter, 1), endpoint)) {
            std::cout << letter;
        }
    }
    catch (std::exception &e) {
        std::cerr << e.what() << std::endl;
    }

    return 0;
}

Additionally I'm just modeling the above code off of the boost udp client code in their tutorial seen here:

//
// client.cpp
// ~~~~~~~~~~
//
// Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#include <iostream>
#include <boost/array.hpp>
#include <boost/asio.hpp>

using boost::asio::ip::udp;

int main(int argc, char* argv[])
{
  try
  {
    if (argc != 2)
    {
      std::cerr << "Usage: client <host>" << std::endl;
      return 1;
    }

    boost::asio::io_context io_context;

    udp::resolver resolver(io_context);
    udp::endpoint receiver_endpoint =
      *resolver.resolve(udp::v4(), argv[1], "daytime").begin();

    udp::socket socket(io_context);
    socket.open(udp::v4());

    boost::array<char, 1> send_buf  = {{ 0 }};
    socket.send_to(boost::asio::buffer(send_buf), receiver_endpoint);

    boost::array<char, 128> recv_buf;
    udp::endpoint sender_endpoint;
    size_t len = socket.receive_from(
        boost::asio::buffer(recv_buf), sender_endpoint);

    std::cout.write(recv_buf.data(), len);
  }
  catch (std::exception& e)
  {
    std::cerr << e.what() << std::endl;
  }

  return 0;
}

I have a suspicion that the tutorial code just doesn't work for my case? Not sure what's wrong or what's the difference. What's the right way to make a standard udp echo client?

Upvotes: 0

Views: 1727

Answers (1)

John Park
John Park

Reputation: 1764

I tested the following code successfully receives message. udp::resolver in your code is not necessary since you're not sending any message, but receiving.

When opening a socket in your case, you should designate a port number in which UDP is expecting data. This can be done by passing a second argument to udp::socket. The second example in your post, however, does not require to do so, because it is reusing its endpoint - sender_endpoint.

receive_from() takes its second argument to store sender's endpoint. You should not pass your peer's endpoint, but a variable to keep where the message came from.

int main(int argc, char* argv[]) {
    try {
        if (argc != 3) {
            std::cerr << "Usage: client <host> <port>" << std::endl;
            return 1;
        }

        boost::asio::io_context ioContext;
        udp::socket socket(ioContext, udp::endpoint(udp::v4(), 6078));  // designated port
        
        char letter = 0;
        udp::endpoint ep_sender;
        while (socket.receive_from(boost::asio::buffer(&letter, 1), ep_sender)) {
            std::cout << letter;
            //std::cout << ep_sender.address().to_string() << endl; // sender's address.
        }

        return 0;
    }
    catch (std::exception& e) {
        std::cerr << e.what() << std::endl;
    }
}

Upvotes: 2

Related Questions