简体   繁体   English

C++ udp boost 客户端没有收到 udp 消息

[英]C++ udp boost client doesn't receive udp messages

When I run:当我跑步时:

nc -ul <ip address> <port>

I receive messages from the udp server.我从 udp 服务器收到消息。 However when I try to reproduce the equivalent using the below C++ program with boost it just freezes on receive_from:但是,当我尝试使用带有 boost 的以下 C++ 程序重现等效项时,它只是在 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:此外,我只是在此处看到的教程中从 boost udp 客户端代码对上述代码进行建模:

//
// 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?制作标准 udp 回显客户端的正确方法是什么?

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.您的代码中的udp::resolver不是必需的,因为您没有发送任何消息,而是接收消息。

When opening a socket in your case, you should designate a port number in which UDP is expecting data.在您的情况下打开套接字时,您应该指定一个端口号,其中 UDP 需要数据。 This can be done by passing a second argument to udp::socket .这可以通过将第二个参数传递给udp::socket来完成。 The second example in your post, however, does not require to do so, because it is reusing its endpoint - sender_endpoint .但是,您帖子中的第二个示例不需要这样做,因为它正在重用其端点 - sender_endpoint

receive_from() takes its second argument to store sender's endpoint. receive_from()使用它的第二个参数来存储发送者的端点。 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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM