我正在尝试编写一个简单的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;
}
任何帮助都将不胜感激。
答案 0 :(得分:2)
10014是WSAEFAULT
,这意味着“optval参数指向的缓冲区不在进程地址空间的有效部分或者optlen参数太小。“的
阅读文档:
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_MEMBERSHIP
和IP_DROP_MEMBERSHIP
。
ip_mreq_source
用于IP_ADD_SOURCE_MEMBERSHIP
,IP_DROP_SOURCE_MEMBERSHIP
,IP_BLOCK_SOURCE
和IP_UNBLOCK_SOURCE
。