gpt4 book ai didi

c++ - 尝试连接到 tcp 套接字时连接被拒绝 (linux)

转载 作者:太空宇宙 更新时间:2023-11-04 10:16:50 26 4
gpt4 key购买 nike

我正在使用 VM ware 开发 ubunto linux 版本 14.04 (linux)

我在 ubunto 14.04 (linux) 下用 c++ 创建了 TCP socket但是当我尝试使用套接字的“连接”方法时,它失败并说连接被拒绝。我试图关闭我的 Windows (10) 计算机中的防火墙,但它根本没有帮助。

非常感谢您的帮助!

TCPSocket::TCPSocket(string peerIp, int port) {
// Open TCP socket
this->connected_sock = socket(AF_INET, SOCK_STREAM, 0);
if (this->connected_sock < 0) {
perror("Error opening channel");
}

// Set the peer address to connect to
bzero(&this->peerAddr, sizeof(this->peerAddr));
this->peerAddr.sin_family = AF_INET;
this->peerAddr.sin_addr.s_addr = inet_addr(peerIp.c_str());
this->peerAddr.sin_port = htons(port);

// FAILED HERE
if (connect(this->connected_sock, (struct sockaddr *) &this->peerAddr,
sizeof(this->peerAddr)) < 0) {
perror("Error establishing communications");
throw "Error establishing communications";
}
}

最佳答案

您的代码没有任何问题。 connection refused意味着沿着路径的某个地方有人将 TCP 重置数据包发送回原始源。在显着点使用网络流量监控工具(例如 tcpdump、wireshark)来追踪失败的地方。 tcpdump 示例:tcpdump -i <interface_name> -nnvvvXX host <destination_ip> and tcp port <destination_port>

关于c++ - 尝试连接到 tcp 套接字时连接被拒绝 (linux),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45773779/

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