ASIO 客户端服务器在同一台 PC 上连接正常,但在本地网络上出现故障

ASIO Client Server Connects Fine on the same PC, but Fails across a Local Network

本文关键字:连接 本地网络 故障 PC 服务器 客户端 一台 ASIO      更新时间:2023-10-16

我将包括代码的所有相关部分,但我很确定问题出在我的网络上,而不是我的代码上,因为如果我在一台计算机上环回,我的代码可以正常工作。不过,如果您在代码中看到任何立即明显的错误,我想知道。

参考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 作为服务器绑定终结点将不起作用,反之亦然,但如果它们都使用 IPv6 或 IPv4,则在环回上工作正常。这些都不适用于不同的计算机。

有什么想法是出了什么问题吗?

正如我们在评论中发现的那样,问题出在网络防火墙加上仅侦听本地主机。侦听 IP 时,可以使用侦听0.0.0.0 IP,或者如果使用终结点,请在不指定侦听 IP 地址的情况下创建它:ip::tcp::endpoint( ip::tcp::v4(), 6118 )