Windows 提升 asio : 10061 in async_receive_from on on async_send_to

标签 windows network-programming udp boost-asio

我有一个相当大的应用程序,可以在 Linux 上正常运行。我最近使用 VC2012 和 boost asio 1.52 在 Windows 7 上编译它并遇到了一个奇怪的问题:

async_receive_from 后跟 async_send_to 在同一 UDP 套接字上导致读取完成处理程序被调用 boost::system::error_code 10061:

No connection could be made because the target machine actively refused it

如果发送目的地是本地主机上的另一个端口。如果将数据包发送到另一台机器,则不会调用读取完成处理程序。在读取完成处理程序之后,写入完成处理程序被调用且没有错误。

以下代码重现了这个问题:

#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>

using namespace std;
using namespace boost::asio;

void read_completion_handler(const boost::system::error_code& ec, std::size_t bytes_received)
{
  if (!ec)
    cout << "Received " << bytes_received << " successfully" << endl;
  else
    cout << "Error: " << ec.message() << endl;
}

void write_completion_handler(const boost::system::error_code& ec, std::size_t bytes_transferred)
{
  if (!ec)
    cout << "Wrote " << bytes_transferred << " successfully" << endl;
  else
    cout << "Error: " << ec.message() << endl;
}

int main(int argc, char** argv)
{
  enum
  {
    max_length = 1500,
    out_length = 100
  };
  // buffer for incoming data
  char data[max_length];
  // outgoing data
  char out_data[out_length];

  // sender endpoint
  ip::udp::endpoint sender_endpoint;
  // for sending packets: if this localhost, the error occurs
  ip::udp::endpoint destination(ip::address::from_string("127.0.0.1"), 5004);

  io_service ioService;
  ip::udp::socket socket(ioService, ip::udp::endpoint(ip::udp::v4(), 49170));

  socket.async_receive_from(
    buffer(data, max_length), sender_endpoint,
    boost::bind(&read_completion_handler, 
    boost::asio::placeholders::error,
    boost::asio::placeholders::bytes_transferred));

  socket.async_send_to( boost::asio::buffer(out_data, out_length),
    destination,
    boost::bind(&write_completion_handler,
    boost::asio::placeholders::error,
    boost::asio::placeholders::bytes_transferred));

  ioService.run();

  cout << "Done" << endl;
  return 0;
}

在 Linux 上这从来都不是问题。有人有解释吗?据我所知,在同一个套接字上同时读写应该没问题,或者在 Windows 上不是这样吗?如果 localhost 是目标,为什么行为会发生变化?

最佳答案

是的,在您提出这个问题后大约 6 个月。我什至不确定我是怎么来到这里的。我自己也遇到过这个问题 - 但好消息是这不是问题。

有些机器在没有监听您将消息发送到的端口时,会通过 ICMP 返回 Destination Unreachable 消息。 Asio 将其转换为 boost::system::errc::connection_refused 和/或 boost::system::errc::connection_reset。这是一个无意义的错误,因为 UDP 是无连接的。您可以安全地忽略 async_receive_from 处理程序中的这两个错误代码(即,如果返回这些错误之一,只需再次调用 async_receive_from)。

关于Windows 提升 asio : 10061 in async_receive_from on on async_send_to,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13995830/

相关文章:

eclipse - 在其他软件中接收 Matlab/simulink UDP block

windows - 在没有 acrobat.exe 的情况下从 VBscript 或命令行打印 PDF

windows - 如何连接到 Windows 资源管理器(用于基于网络的文件存储,例如 Dropbox)

windows - 了解 cmd shell 中的变量

java - Tizen 包管理器无法识别 JDK

Java聊天服务器

当前网络流量的android事件?

C : Segmentation fault with typecasting

java - 如何在不丢失数据包的情况下通过打洞 (STUN) UDP 发送大文件?

c# - UWP App 停止接收 UDP 多播消息