setsockopt返回错误10014

时间:2014-03-04 01:13:25

标签: c++ windows sockets multicast setsockopt

我正在尝试编写一个简单的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;
    }

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:2)

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