setsockopt 返回错误 10014

Posted

技术标签:

【中文标题】setsockopt 返回错误 10014【英文标题】:setsockopt returning error 10014 【发布时间】:2014-03-04 01:13:25 【问题描述】:

我正在尝试编写一个简单的 UDP 服务器,它在多播组上通告其服务端口,但我无法让多播工作。我尝试查看 Microsoft 文档并复制/粘贴了他们的一些代码,但它不起作用。这是有问题的代码:

    this->port = srvc_port; //11129 in my example
    this->mcast_port = mcast_port; //11130 in my example
    this->mcast_ip = mcast_ip; //230.0.0.30 in my example

    int iResult = WSAStartup(MAKEWORD(2,2), &service_wsaData);
    if (iResult != NO_ERROR)
    
        cout << "WSAstartup failed" << endl;
        return -1;
    

    service_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (service_sock == INVALID_SOCKET) 
        closesocket(service_sock);
        WSACleanup();
        cout << "Error at socket" << endl;
        return -1;
    

    mcast_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if(mcast_sock == INVALID_SOCKET) 
        closesocket(mcast_sock);
        WSACleanup();
        cout << "Error at socket" << endl;
        return -1;
    

    multicast.sin_family = AF_INET; // multicast is a sockaddr_in struct
    multicast.sin_addr.s_addr = INADDR_ANY;
    multicast.sin_port = htons(mcast_port);

    service.sin_family = AF_INET; //same for service
    service.sin_addr.s_addr = INADDR_ANY;
    service.sin_port = htons(port);

    if(::bind( service_sock, (sockaddr *)&service, sizeof(service) ) == SOCKET_ERROR) 
        closesocket(mcast_sock);
        closesocket(service_sock);
        WSACleanup();
        cout << "Error binding service socket" << endl;
        return -1;
    ;

    if(::bind( mcast_sock, (sockaddr *)&multicast, sizeof(multicast) ) == SOCKET_ERROR) 
        closesocket(mcast_sock);
        closesocket(service_sock);
        WSACleanup();
        cout << "Error binding multicast socket" << endl;
        return -1;
    ;

    mreq.imr_multiaddr.s_addr = inet_addr(mcast_ip.c_str()); //mreq is declared like so "struct ip_mreq mreq;" in the class
    mreq.imr_interface.s_addr = htonl(INADDR_ANY);

    if(setsockopt(mcast_sock,IPPROTO_IP,IP_ADD_SOURCE_MEMBERSHIP,(char *) &mreq, sizeof(mreq)) == SOCKET_ERROR)
    
            //I get error 10014 here
        cout << "Error: " << WSAGetLastError() << endl;
        return -1;
    

任何帮助将不胜感激。

【问题讨论】:

好吧,文档说 10014 表示地址错误。我们从您的 mreq 变量及其初始化中看不到太多,在这里您只初始化 3 个成员中的 2 个。在套接字编程的情况下,通过将这些结构的初始化设置为零也是明智的。 【参考方案1】:

10014是WSAEFAULT,表示“optval参数指向的缓冲区不在进程地址空间的有效部分或者optlen参数太小。

阅读文档:

IPPROTO_IP Socket Options

IP_ADD_SOURCE_MEMBERSHIP 需要一个指向 ip_mreq_source 结构的指针:

typedef struct ip_mreq_source 
  struct in_addr  imr_multiaddr;
  struct in_addr  imr_sourceaddr;
  struct in_addr  imr_interface;
 IP_MREQ_SOURCE, *PIP_MREQ_SOURCE;

您正在向它传递一个指向 ip_mreq 结构的指针:

typedef struct ip_mreq 
  struct in_addr  imr_multiaddr;
  struct in_addr  imr_interface;
 IP_MREQ, *PIP_MREQ;

ip_mreq 小于ip_mreq_source,因此sizeof(mreq) 对于IP_ADD_SOURCE_MEMBERSHIP 来说太小了。您需要将您的mreq 变量更改为ip_mreq_source,并确保填写其imr_sourceaddr 成员。

ip_mreq 用于IP_ADD_MEMBERSHIPIP_DROP_MEMBERSHIP

ip_mreq_source 用于IP_ADD_SOURCE_MEMBERSHIPIP_DROP_SOURCE_MEMBERSHIPIP_BLOCK_SOURCEIP_UNBLOCK_SOURCE

【讨论】:

以上是关于setsockopt 返回错误 10014的主要内容,如果未能解决你的问题,请参考以下文章

Hive 给出 SemanticException [错误 10014]:运行我的 UDF 时

失败:语义异常 [错误 10014]:第 1:21 行错误的参数“stock_price_high”:

windows winsock2 socket从WSAGetLastError返回10014 WSAEFAULT(ipv4 / ipv6相关)

getsockopt 返回与 setsockopt 中的一组不同的 IP_TOS 值

setsockopt

UVA-10014