Boost的UDP异步客户端接收自己的数据报 [英] Boost's UDP asynchronous client receiving its own datagram

查看:163
本文介绍了Boost的UDP异步客户端接收自己的数据报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Boost :: asio .我已经阅读了文档和官方教程,但是仍然无法正确理解.

I am trying to make a simple UDP client/server test using Boost::asio. I've already read the documentation and the official tutorials, but I still couldn't get it right.

我的问题专门在客户端.这是我希望客户端执行的操作:它必须向运行在localhost:12345的服务器发送一个简单的数据报,然后再侦听服务器发送的响应数据报.不过,那不是我得到的.看起来客户端正在发送数据报,但是在它收到自己的数据报之后!在有人问之前,不,服务器没有运行:-)

My problem is specifically on the client side. Here's what I want the client to do: it has to send a simple datagram to the server running at localhost:12345 and then subsequently listen for the response datagram that the server sends. That's not what I am getting, though. Looks like the client is sending the datagram alright, but right after it receives its own datagram! And before someone asks, no, the server is not running :-)

函数send_datagram()似乎正确发送了数据报,但是async_receive_from()似乎是由该发送的数据报触发的.这是客户端的完整源代码:

The function send_datagram() seems to correctly send the datagram, but then async_receive_from() seems to be triggered by that sent datagram. Here's the complete source code for the client:

#include <boost/array.hpp>
#include <boost/asio/error.hpp>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/format.hpp>
#include <iostream>

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

const char *SERVER_ADDRESS = "localhost";
const char *SERVER_PORT = "12345";

boost::asio::io_service io_service;

udp::resolver resolver(io_service);
udp::resolver::query query(udp::v4(), SERVER_ADDRESS, SERVER_PORT);
udp::endpoint server_endpoint = (udp::endpoint) *resolver.resolve(query);
udp::endpoint remote_endpoint;
boost::asio::ip::udp::socket client_socket(io_service, server_endpoint);
boost::array<char, 2048> recv_buffer;

boost::shared_ptr<std::string> message(new std::string("test"));

void handle_recv(const boost::system::error_code& error, std::size_t /* bytes_transferred */) {

    if (error)
        std::cout << error.message() << std::endl;
    else {
        std::cout << "Datagram received" << std::endl;
    }
}

void handle_send(boost::shared_ptr<std::string> /* message */, const boost::system::error_code& error, std::size_t /* bytes_transferred */) {

    if (error)
        std::cout << error.message() << std::endl;
    else {
        std::cout << "Datagram sent" << std::endl;
    }
}

void recv_datagram() {

    client_socket.async_receive_from(
            boost::asio::buffer(recv_buffer),
            remote_endpoint,
            boost::bind(&handle_recv, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)
    );
}

void send_datagram() {

    client_socket.async_send_to(
            boost::asio::buffer(*message),
            server_endpoint,
            boost::bind(&handle_send, message, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)
    );
}

int main(int argc, char** argv) {

    std::cout << "Server address: " << server_endpoint.address() << std::endl;
    std::cout << "Server port: " << server_endpoint.port() << std::endl;
    std::cout << "Remote address: " << remote_endpoint.address() << std::endl;
    std::cout << "Remote port: " << remote_endpoint.port() << std::endl;

    send_datagram();
    recv_datagram();

    io_service.run();

    std::cout << "Done" << std::endl;

    return 0;
}

这是我得到的输出:

Server address: 127.0.0.1
Server port: 12345
Remote address: 0.0.0.0
Remote port: 0
Datagram sent
Datagram received
Done

同样,服务器在我的测试过程中没有运行(实际上甚至还没有实现!).

Again, the server was not running during my tests (in fact it isn't even implemented yet!).

我认为我正在做一些可怕的愚蠢的事情,但是我仍然没有弄清楚它是什么.

I think I'm doing something terrible stupid, but I still haven't figured out what it is.

推荐答案

是的,所以我发现了我的问题所在:

Yeah, so I found what was my problem:

boost::asio::ip::udp::socket client_socket(io_service, server_endpoint);

我意外地将客户端套接字绑定到服务器端点,因此它有效地在端口12345上运行.当我调用async_receive_from()时,套接字正在侦听端口12345(该端口应该运行服务器).

I accidentaly bound my client socket to the server endpoint, so it is effectively running on port 12345. When I call async_receive_from(), the socket is listening on port 12345, the same port supposed to run the server.

所以解决方案是简单地定义一个新的端点,并将其传递给套接字构造:

So the solution was to simply define a new endpoint and pass it in the socket construction:

udp::endpoint local_endpoint;
boost::asio::ip::udp::socket client_socket(io_service, local_endpoint);

现在的输出是:

Server address: 127.0.0.1
Server port: 12345
Remote address: 0.0.0.0
Remote port: 0
Datagram sent

现在看起来它正在正确地等待服务器响应.

And now looks like it is correctly waiting for the server response.

只是为了试图弄清正在发生的事情,当创建客户端套接字时,必须将其绑定到某个本地端口.当创建不带参数的local_endpoint时,它将在没有特定网络接口的情况下生成一个指向端口0的端点.这是$BOOST_ROOT/boost/asio/ip/detail/impl/endpoint.ipp中的构造函数实现:

Just to try to clarify what is happening, when the client socket is created, it must be bound to some local port. When local_endpoint is created without parameters, it generates an endpoint pointing to port 0 at no specific network interface. Here's the constructor implementation as in $BOOST_ROOT/boost/asio/ip/detail/impl/endpoint.ipp:

endpoint::endpoint()
  : data_()
{
  data_.v4.sin_family = BOOST_ASIO_OS_DEF(AF_INET);
  data_.v4.sin_port = 0;
  data_.v4.sin_addr.s_addr = BOOST_ASIO_OS_DEF(INADDR_ANY);
}

当套接字构造函数收到此特殊端点时,它会要求操作系统选择一个随机端口,以便套接字可以绑定到该端口.要查看那是哪个端口:

When the socket constructor receives this special endpoint, it asks for the operating system to choose a random port so that the socket can bind to. To see what port was that:

std::cout << "Local address: " << client_socket.local_endpoint().address() << std::endl;
std::cout << "Local port: " << client_socket.local_endpoint().port() << std::endl;

示例输出:

Local address: 0.0.0.0
Local port: 59908

这篇关于Boost的UDP异步客户端接收自己的数据报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆