gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 02:51:19 25 4
gpt4 key购买 nike

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

引用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 或者如果你使用 endpoint 时,可以监听任何 IP,创建它而不指定监听 IP 地址:ip::tcp::端点( ip::tcp::v4(), 6118 )

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com