我正在使用C ++网络,几乎编写了我的第一个程序,包括一个服务器和一个相互连接的客户端。这是我的代码 -
server.cpp
#include "stdafx.h"
using namespace std;
int main()
{
int wsasuccessful;
WSAData WinSockData;
WORD DLLVERSION;
DLLVERSION = MAKEWORD(2, 1);
wsasuccessful = WSAStartup(DLLVERSION, &WinSockData);
if (wsasuccessful != 0)
{
printf("Error %d while starting WSA\n", wsasuccessful);
pause();
return 1;
}
SOCKADDR_IN ADDRESS;
int AddressSize = sizeof(ADDRESS);
SOCKET uc_socket;
uc_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
ADDRESS.sin_addr.s_addr = INADDR_ANY;
ADDRESS.sin_family = AF_INET;
ADDRESS.sin_port = htons(444);
if (WSAGetLastError() != 0)
{
printf("Error %d while establishing socket\n", WSAGetLastError());
pause();
return 1;
}
bind(uc_socket, (SOCKADDR *)&ADDRESS, AddressSize);
if (WSAGetLastError() != 0)
{
printf("Error %d while binding socket\n", WSAGetLastError());
pause();
return 1;
}
listen(uc_socket, 12);
if (WSAGetLastError() != 0)
{
printf("Error %d while binding socket\n", WSAGetLastError());
pause();
return 1;
}
SOCKET c_socket;
SOCKADDR client_sock;
int client_sock_size = sizeof(client_sock);
printf("Socket created; Set to listen for incoming connections");
c_socket = accept(uc_socket, (SOCKADDR *)&client_sock, &client_sock_size);
printf("Connection found!");
pause();
return 0;
}
client.cpp
#include "stdafx.h"
using namespace std;
int main()
{
int wsasuccessful = -1;
WSAData WinSockData;
WORD DLLVERSION;
DLLVERSION = MAKEWORD(2, 1);
wsasuccessful = WSAStartup(DLLVERSION, &WinSockData);
if (wsasuccessful != 0)
{
printf("Error %d in client while starting WSA\n", WSAGetLastError());
pause();
return 1;
}
SOCKET client_socket;
client_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (WSAGetLastError() != 0)
{
printf("Error %d in client while instantiating socket\n", WSAGetLastError());
pause();
return 1;
}
SOCKADDR_IN client_connection;
client_connection.sin_family = AF_INET;
client_connection.sin_port = (USHORT)(444);
client_connection.sin_addr.S_un.S_addr = (unsigned long)("0.0.0.0");
int connection_success = -1;
printf("Connection started!");
connection_success = connect(client_socket, (SOCKADDR *)&client_connection, sizeof(client_connection));
if (connection_success != 0)
{
printf("Error %d in client while instantiating socket\n", WSAGetLastError());
pause();
return 2;
}
printf("The client has found a server!");
printf("The last WSA error was: %d", WSAGetLastError());
pause();
return 0;
}
多次重复后我没有看到任何错误,但是启动后我都没有连接(当然是服务器)
我已设置防火墙以允许连接到网络
这两个二进制文件听不到(当然)和连接。服务器和客户端。
我检查了正在使用的端口和关联的PID(netstat -a -o),果然,服务器正在侦听端口444,如代码中所述。
有没有人对可能导致此问题的原因有任何想法?
答案 0 :(得分:3)
0.0.0.0
(又名INADDR_ANY
)不是connect()
到的客户的有效IP地址。如果服务器和客户端在同一台计算机上运行,则可以使用127.0.0.1
(又名INADDR_LOOPBACK
),因为您的服务器绑定到INADDR_ANY
,其中包含127.0.0.1
。< / p>
此外,这些陈述在客户端是错误的:
client_connection.sin_port = (USHORT)(444);
client_connection.sin_addr.S_un.S_addr = (unsigned long)("0.0.0.0");
您需要使用inet_addr()
(或等效内容)将IP地址从字符串格式转换为二进制格式。而且你需要使用htons()
,就像你在服务器端那样:
client_connection.sin_port = htons(444);
client_connection.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
此外,您的错误处理通常是错误的。除非事先报告错误,否则不要致电WSAGetLastError()
。这意味着检查socket()
的{{1}}和accept()
的返回值,INVALID_SOCKET
,bind()
和listen()
的返回值connect()
1}}(即-1,而不是0),等等SOCKET_ERROR
可能会失败并且您没有正确检测到错误,因此您认为它确实失败时会成功。