c++ - ASIO 客户端服务器在同一台 PC 上连接正常,但在本地网络上失败

标签 c++ networking tcp server boost-asio

我将包含代码的所有相关部分,但我很确定问题出在我的网络上,而不是我的代码上,因为如果我在一台计算机上环回,我的代码工作得很好。不过,如果您在代码中发现任何明显的错误,我想知道。

引用basic_server代码(仅相关部分):

connection_id basic_server::start_listening(const std::string & ip_address, const std::string & port) {
    asio::ip::tcp::resolver resolver(*service);
    asio::ip::tcp::resolver::query query(ip_address, port);
    asio::ip::tcp::endpoint endpoint = *(resolver.resolve(query));

    connection_id id(get_seed());
    while (connections.contains(id)) {
        std::this_thread::yield();
        id = get_seed();
    }
    connections[id] = connection_ptr(new connection(id, *service, ip_address, port));
    connection_ptr conn = connections[id];
    conn->state = listening;

    if (!acceptor->is_open()) {
        acceptor->open(endpoint.protocol());
    }
    if (current_ip != ip_address || current_port != port) {
        acceptor->bind(endpoint);
        current_ip = ip_address;
        current_port = port;
    }
    acceptor->listen(asio::socket_base::max_connections);

    acceptor->async_accept(
        conn->get_socket(),
        std::bind(
            &basic_server::connect,
            this,
            std::placeholders::_1,
            id
        )
    );
    return id;
}

connection_id basic_server::start_connecting(const std::string & ip_address, const std::string & port) {
    asio::ip::tcp::resolver resolver(*service);
    asio::ip::tcp::resolver::query query(ip_address, port);
    asio::ip::tcp::endpoint endpoint = *(resolver.resolve(query));

    connection_id id(get_seed());
    while (connections.contains(id)) {
        std::this_thread::yield();
        id = get_seed();
    }
    connections[id] = connection_ptr(new connection(id, *service, ip_address, port));
    connection_ptr conn = connections[id];
    conn->state = connecting;

    conn->get_socket().async_connect(
        endpoint,
        std::bind(
            &basic_server::connect,
            this,
            std::placeholders::_1,
            id
        )
    );
    return id;
}

引用服务器代码:

#include "../../Utilities/BasicServer/Basic Server.h"

int main() {
    server::basic_server this_server;
    server::connection_id id = this_server.start_listening("::1", "6118");
    const std::set<server::connection_state> valid_states = { server::open, server::listening, server::connecting };
    while (this_server.connection_status(id) == server::listening) std::cout << "Waiting for Client.\r";
    std::cout << std::endl;
    while (true) {
        if (valid_states.find(this_server.connection_status(id)) == valid_states.end()) {
            std::cout << "We've lost connection with the client." << std::endl;
            break;
        }
        server::data_pair data;
        bool successful_read = this_server.read_from_queue(data);
        if (!successful_read) {
            std::this_thread::yield();
            continue;
        }
        server::connection_id read_id = data.first;
        server::data_vector & read_vector = data.second;
        std::string line;
        line.resize(70);
        std::copy(read_vector.begin(), read_vector.begin() + std::min(70ull, read_vector.size()), line.begin());
        std::cout << line << std::endl;
    }
    system("pause");
    return 0;
}

引用客户端代码(与服务器代码没有太大区别):

#include "../../Utilities/BasicServer/Basic Server.h"

int main(int argc, char ** argv) {
    server::basic_server this_client;
    std::string ip_address;
    if (argc < 2) return 0;
    ip_address = argv[1];
    server::connection_id id = this_client.start_connecting(ip_address, "6118");
    const std::set<server::connection_state> valid_states = { server::open, server::listening, server::connecting };
    while (this_client.connection_status(id) == server::connecting) std::cout << "Connecting to Server with IP address \"" << ip_address << "\"\r";
    std::cout << std::endl;
    if (this_client.connection_status(id) == server::open) {
        std::cout << "We're connected!" << std::endl;
    }
    else {
        std::cout << "Unable to connect." << std::endl;
        system("pause");
        return 0;
    }
    while (true) {
        if (valid_states.find(this_client.connection_status(id)) == valid_states.end()) {
            std::cout << "We've lost connection." << std::endl;
            break;
        }
        std::string line;
        std::getline(std::cin, line);
        std::cout << "Attemping to write \"" << line << "\"" << std::endl;
        this_client.write_to_connection(id, &line.front(), line.size() * sizeof(unsigned char));
        if (line == "") break;
    }
    system("pause");
    return 0;
}

所以我的问题的基本要点是,当我尝试从一台计算机连接到另一台计算机(通过我的本地网络)时,连接失败。如果我在同一台计算机上运行服务器和客户端,则它可以正常工作。我已尝试以下所有方法:

  • 对接收/发送计算机执行 ping 操作以验证它们是否能够看到对方:确实如此。
  • 运行 tracert 来检查连接:它们在一跳中相互到达,不涉及外部连接。
  • 在使用 IPv6 和 IPv4(我的路由器支持两者)之间切换来尝试连接:我确定,在环回时,如果客户端使用 IPv4 环回地址,则使用 IPv6 作为服务器绑定(bind)端点将不起作用,反之亦然。反之亦然,但如果它们都使用 IPv6 或 IPv4,则它在环回上工作正常。这些都不能在不同的计算机上工作。

对于出了什么问题有什么想法吗?

最佳答案

正如我们在评论中发现的,问题是网络防火墙加上仅监听本地主机。当您监听0.0.0.0 IP时,可以监听任何IP,或者如果您使用端点,则在不指定监听IP地址的情况下创建它:ip::tcp::端点( ip::tcp::v4(), 6118 )

关于c++ - ASIO 客户端服务器在同一台 PC 上连接正常,但在本地网络上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35497105/

相关文章:

c++ - 防止在 Windows 中取消停靠计算机

c++ - 为什么 C++ 析构函数在继承时被调用 2 次

c# - 创建FileStream进行网络共享非常慢

android-studio - 在 Android Studio 中通过 tcpip 使用 adb?

python - 在 Python 中创建应用程序级数据包

c++ - 删除存储在数组中的特定类对象

c++ - future 、 promise 和异常(exception)

android - Apache Cordova : how to block network access in config. xml

networking - 处理实时游戏中的网络丢包——TCP和UDP

ssl - SSL session 缓存和过期如何工作