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

时间:2016-02-19 04:06:01

标签: 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作为服务器绑定端点将无法正常工作,并且反之亦然,但如果他们都使用IPv6或IPv4,它在环回上工作正常。这些都不适用于不同的计算机。

关于出了什么问题的任何想法?

1 个答案:

答案 0 :(得分:1)

正如我们在评论中发现的那样,问题是网络防火墙加上仅侦听localhost。当您收听0.0.0.0 IP或使用端点时,可以通过聆听任何IP,无需指定侦听IP地址即可创建它:ip::tcp::endpoint( ip::tcp::v4(), 6118 )