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

查看:120
本文介绍了升压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.

我的问题是专门在客户端。这是我想在客户端做的事:它必须发送一个简单的数据包到服务​​器在运行本地主机: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()似乎被触发通过发送数据报。下面是完整的源代码code客户端:

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 /升压/ ASIO / IP /细节/ 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

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

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