为什么这个 boost asio 代码不能正常工作?

发布于 2024-09-24 07:03:29 字数 1447 浏览 1 评论 0原文

此 boost udp 服务器未按预期运行。

除了细微的变化外,它与阻塞 UDP 回显服务器相同。 我使用不同的套接字来返回响应,即 sock2。 现在,如果客户端与服务器位于同一台计算机上,则此代码可以完美运行。 但是当我在另一台机器上运行它时,没有收到响应。 但是,如果我将发送套接字更改为 sock 而不是 sock2 它确实可以工作 跨机器。

知道为什么会这样吗? wireshark 根本没有显示任何错误。客户端使用随机源端口,但随后在同一随机端口上调用recv_from。服务器将响应发送回客户端随后侦听的同一端口号。

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

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

enum { max_length = 1024 };

void server(boost::asio::io_service& io_service, short port)
{
  udp::socket sock(io_service, udp::endpoint(udp::v4(), port));
  udp::socket sock2(io_service, udp::endpoint(udp::v4(), 0));
  for (;;)
  {
    char data[max_length];
    udp::endpoint sender_endpoint;
    size_t length = sock.receive_from(
        boost::asio::buffer(data, max_length), sender_endpoint);
    printf("Got data, sending response to %s:%d\n", sender_endpoint.address().to_string().c_str(), sender_endpoint.port());
    sock2.send_to(boost::asio::buffer(data, length), sender_endpoint);
  }
}

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

    boost::asio::io_service io_service;

    using namespace std; // For atoi.
    server(io_service, atoi(argv[1]));
  }
  catch (std::exception& e)
  {
    std::cerr << "Exception: " << e.what() << "\n";
  }

  return 0;
}

This boost udp server doesn't function as expected.

It is identical to the blocking UDP echo server EXCEPT for a minor change.
I'm using a different socket to return the response, i.e. sock2.
Now this code works perfectly if the client is on the same machine as the server.
But the moment I run this on a different machine, the response is not received.
However, if I change the sending socket to be sock rather than sock2 it does work
across machines.

Any idea why that would be? wireshark is not showing any errors at all. Client uses random source port, but then calls recv_from on the same random port. Server sends response back to that same port number that the client then listens on.

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

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

enum { max_length = 1024 };

void server(boost::asio::io_service& io_service, short port)
{
  udp::socket sock(io_service, udp::endpoint(udp::v4(), port));
  udp::socket sock2(io_service, udp::endpoint(udp::v4(), 0));
  for (;;)
  {
    char data[max_length];
    udp::endpoint sender_endpoint;
    size_t length = sock.receive_from(
        boost::asio::buffer(data, max_length), sender_endpoint);
    printf("Got data, sending response to %s:%d\n", sender_endpoint.address().to_string().c_str(), sender_endpoint.port());
    sock2.send_to(boost::asio::buffer(data, length), sender_endpoint);
  }
}

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

    boost::asio::io_service io_service;

    using namespace std; // For atoi.
    server(io_service, atoi(argv[1]));
  }
  catch (std::exception& e)
  {
    std::cerr << "Exception: " << e.what() << "\n";
  }

  return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

虐人心 2024-10-01 07:03:29

再次回答我自己的问题!

检查另一台机器上没有运行防火墙软件!。事实证明,VM 中运行的 Windows 机器正在运行 Windows 防火墙。

我没有立即怀疑这一点,因为当我使用传递到服务器的端口号在原始套接字上发送 UDP 响应时,它起作用了。

我猜想 Windows 防火墙正在维护某种状态。

Answering my own question again!

Check there is no firewall software running on the other machine!. It turns out that the windows machine running in the VM was running a windows firewall.

I didn't immediately suspect this because when I send the UDP response on the original socket using the port number passed into the server, it worked.

I guess the windows firewall is maintaining some state.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文