尝试连接到 TCP 套接字 (Linux) 时连接被拒绝

Connection Refused when trying to connect to tcp socket (linux)

本文关键字:连接 Linux 拒绝 套接字 TCP      更新时间:2023-10-16

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

我已经在 ubunto 14.04 (linux( 下用 c++ 创建了 TCP 套接字 但是当我尝试使用套接字的"连接"方法时,它失败并说 连接拒绝。我试图关闭我的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>

所以我解决了这个问题!

我哑巴...

灵魂在调用=>我调用了不同的端口。

服务器侦听端口 13301 并且客户端需要连接到同一端口, 只有这样,服务器才会接受客户端并与他一起打开新的套接字。

非常感谢您的帮助!