場景服務器
須要開發一個服務器發現模塊,等待客戶端的探測報文。在Linux服務器下組播地址imr_interface能夠不綁定socket
mcast.imr_multiaddr.s_addr = inet_addr("239.255.255.250");
mcast.imr_interface.s_addr = htonl(INADDR_ANY);ide
可是在windwos服務器下必須綁定一個指定的IP地址,不然沒法收到任何的組播探測包spa
具體代碼code
//加入設備組,設備發現
DWORD WINAPI OnvifBeDiscovered(LPVOID p)
{
struct soap UDPserverSoap;
struct ip_mreq mcast;server
soap_init1(&UDPserverSoap, SOAP_IO_UDP | SOAP_XML_IGNORENS);
soap_set_namespaces(&UDPserverSoap, namespaces);ip
SOAP_SOCKET localSocket = soap_bind(&UDPserverSoap, NULL, 3702, 10);
if (!soap_valid_socket(localSocket))
{
soap_print_fault(&UDPserverSoap, stderr);
exit(1);
}開發
mcast.imr_multiaddr.s_addr = inet_addr("239.255.255.250");
mcast.imr_interface.s_addr = inet_addr("192.168.58.173");
//IP_ADD_MEMBERSHIP用於加入某個多播組,以後就能夠向這個多播組發送數據或者從多播組接收數據
if (setsockopt(UDPserverSoap.master, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&mcast, sizeof(mcast)) < 0)
{
printf("setsockopt error! error code = %d,err string = %s\n", errno, strerror(errno));
return 0;
}string
SOAP_SOCKET clientSocket = -1;
while (1)
{
clientSocket = soap_accept(&UDPserverSoap);
if (!soap_valid_socket(clientSocket))
{
soap_print_fault(&UDPserverSoap, stderr);
exit(1);
}it
if (soap_serve(&UDPserverSoap) != SOAP_OK) { soap_print_fault(&UDPserverSoap, stderr); } soap_destroy(&UDPserverSoap); soap_end(&UDPserverSoap); } soap_done(&UDPserverSoap);}