使用 WinSock 时接收SOCKET_ERROR

Receiving SOCKET_ERROR when using WinSock

本文关键字:SOCKET ERROR WinSock 使用      更新时间:2023-10-16

我环顾了一下论坛,用谷歌搜索了几个小时,但找不到确切的解决方案。如果您能给我一些有关此代码问题的信息,我将不胜感激。我已经尝试了很多东西,但似乎无法正常工作。许多代码取自Microsoft的文章:完整的WinSock客户端代码。

C++中的代码:

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

#include "stdafx.h"
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#pragma comment(lib, "Ws2_32.lib")
int main() {
WSADATA wsaData;
#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "27015"
SOCKET ConnectSocket = INVALID_SOCKET;
struct addrinfo *result = NULL,
    *ptr = NULL,
    hints;
char *sendbuf = "this is a test";
char recvbuf[DEFAULT_BUFLEN];
int iResult;
int recvbuflen = DEFAULT_BUFLEN;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
    printf("WSAStartup failed with error: %dn", iResult);
    return 1;
}
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
// Resolve the server address and port
iResult = getaddrinfo("127.0.0.1", NULL, &hints, &result);
if (iResult != 0) {
    printf("getaddrinfo failed with error: %dn", iResult);
    WSACleanup();
    return 1;
}
// Attempt to connect to an address until one succeeds
for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {
    // Create a SOCKET for connecting to server
    ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,
        ptr->ai_protocol);
    if (ConnectSocket == INVALID_SOCKET) {
        printf("socket failed with error: %ldn", WSAGetLastError());
        WSACleanup();
        return 1;
    }
    // Connect to server.
    iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
    if (iResult == SOCKET_ERROR) {
        closesocket(ConnectSocket);
        ConnectSocket = INVALID_SOCKET;
        continue;
    }
    break;
}
freeaddrinfo(result);
if (ConnectSocket == INVALID_SOCKET) {
    printf("Unable to connect to server!n");
    WSACleanup();
    return 1;
}
// Send an initial buffer
iResult = send(ConnectSocket, sendbuf, (int)strlen(sendbuf), 0);
if (iResult == SOCKET_ERROR) {
    printf("send failed with error: %dn", WSAGetLastError());
    closesocket(ConnectSocket);
    WSACleanup();
    return 1;
}
printf("Bytes Sent: %ldn", iResult);
// shutdown the connection since no more data will be sent
iResult = shutdown(ConnectSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
    printf("shutdown failed with error: %dn", WSAGetLastError());
    closesocket(ConnectSocket);
    WSACleanup();
    return 1;
}
// Receive until the peer closes the connection
do {
    iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
    if (iResult > 0)
        printf("Bytes received: %dn", iResult);
    else if (iResult == 0)
        printf("Connection closedn");
    else
        printf("recv failed with error: %dn", WSAGetLastError());
} while (iResult > 0);
// cleanup
closesocket(ConnectSocket);
WSACleanup();
getchar();
return 0;

}

您忘记指定要连接的 TCP 端口号。这是要getaddrinfo的第二个论点。

例如像这样:

  iResult = getaddrinfo("127.0.0.1", "80", &hints, &result);
相关文章: