我必须在c ++中创建服务器和客户端应用程序,并且服务器必须在客户端连接到服务器时发送数字。我做了这个: 服务器应用程序
// Accept a client socket
ClientSocket = accept(ListenSocket, NULL, NULL);
if (ClientSocket == INVALID_SOCKET) {
printf("accept failed with error: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
// No longer need server socket
closesocket(ListenSocket);
std::clog << "Sending numbery\n";
iSendResult = send(ClientSocket, (const char*)&numberToSend, iResult, 0);
if (iSendResult == SOCKET_ERROR) {
printf("send failed with error: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return 1;
}
和客户端代码
if (ConnectSocket == INVALID_SOCKET) {
printf("Unable to connect to server!\n");
WSACleanup();
return 1;
}
std::clog << "Conneted\n";
do {
std::clog << "Waiting for number\n";
iResult = recv(ConnectSocket, (char*)&recvNumber, sizeof(long long), 0);
if (iResult > 0)
printf("Bytes received: %d\n", iResult);
else if (iResult == 0)
printf("Connection closed\n");
else
printf("recv failed with error: %d\n", WSAGetLastError());
} while (iResult > 0);
当我运行服务器应用程序然后在服务器窗口中运行客户端应用程序我看到“发送号码”,在客户端窗口我看到“已连接等待号码”但在客户端应用程序中我无法接收从服务器发出的号码,任何人都有想法有什么不对?客户端与服务器连接后如何发送数据?