c++ - 连接函数错误 10047 (winsock2)

标签 c++ winapi winsock2

所以我从 MSDN 站点复制了一些使用基本 Windows 套接字功能的测试代码。这是代码:

#include "stdafx.h"

#ifndef UNICODE
#define UNICODE
#endif

#include <stdio.h>
#include <winsock2.h>
#include <ws2tcipip.h>
#include <wchar.h>


int main()
{

    int iResult = 0;

    //----------------------
    // Initialize Winsock
    WSADATA wsaData;
    iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iResult != 0) {
        wprintf(L"WSAStartup function failed with error: %d\n", iResult);
        return 1;
    }
    //----------------------
    // Create a SOCKET for connecting to server
    SOCKET ConnectSocket;
    ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (ConnectSocket == INVALID_SOCKET) {
        wprintf(L"socket function failed with error: %ld\n", WSAGetLastError());
        WSACleanup();
        return 1;
    }
    //----------------------
    // The sockaddr_in structure specifies the address family,
    // IP address, and port of the server to be connected to.

    int I = sizeof(sockaddr_in);
        sockaddr_in clientService;
    clientService.sin_family = AF_INET;     
        clientService.sin_port = htons(5000);
    in_addr *s = (in_addr*)malloc(sizeof(in_addr));
    s->s_addr = inet_addr("127.0.0.1");
    clientService.sin_addr = (in_addr_t)s;

    iResult = connect(ConnectSocket, (sockaddr*)&clientService,I);
    if (iResult == SOCKET_ERROR) {
        wprintf(L"connect function failed with error: %ld\n", WSAGetLastError());
        iResult = closesocket(ConnectSocket);
        if (iResult == SOCKET_ERROR)
            wprintf(L"closesocket function failed with error: %ld\n", WSAGetLastError());
        WSACleanup();
        return 1;
    }

    wprintf(L"Connected to server.\n");

    iResult = closesocket(ConnectSocket);
    if (iResult == SOCKET_ERROR) {
        wprintf(L"closesocket function failed with error: %ld\n", WSAGetLastError());
        WSACleanup();
        return 1;
    }

    WSACleanup();
    return 0;
}

代码编译得很好。但是当我运行该程序时,命令提示符屏幕显示以下错误消息:

connection failed with error: 10047

现在我知道错误 10047 表示地址结构中的错误。我尝试使用 inet_pton,但这会导致段错误(内存访问冲突),因为 inet_pton 使用 memcpy 函数。那么这里发生了什么? connect 功能是否未正确实现?也许还有另一种指定地址结构的方法。

最佳答案

来自 MSDN:http://msdn.microsoft.com/en-us/library/windows/desktop/ms737625%28v=vs.85%29.aspx

sockaddr_in clientService;
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr("127.0.0.1");
clientService.sin_port = htons(27015);

您的设置 .sin_addr.s_addr 似乎模棱两可。

如果上面的结果不是问题,那么也许您启用了 IP6 协议(protocol)但没有启用 IP4,这就是 AF_NET 失败并需要 AF_NET6 的原因。

关于c++ - 连接函数错误 10047 (winsock2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14244939/

相关文章:

c++ - std::shared_ptr 和 std::unique_ptr 构造函数之间的不对称性

c++ - Winsock2,客户端-服务器通信——轮流发送/接收

c++ - recv() 始终返回 0

winapi - InterlockedCompareExchange Release() 和 Acquire() 之间有什么区别?

c++ - 无法在 32 位服务中使用 RegLoadKey 加载 64 位 key

c++ - 当套接字中发送速度太快时连接缓冲区

c++ - Qt 文件对话框呈现不正确并崩溃

c++ - 我怎样才能知道插入的是什么类型的耳机?

c++ - 如何有效地将 git 存储库/子模块用于具有许多依赖项的 C++ 产品?

c++ - wchar_t 和 char 在 WinDbg 中的表示