前言

            之前在第四课实现了服务器加入组播,通知其他客户端服务器的IP地址和服务信息。本节课是绑定本地的端口,接收处理Onvif协议的报文


代码

    

//监听soap报文
DWORD WINAPI OnvifWebServices(LPVOID p)
{

 struct soap tcpSoap = { 0 };
 soap_init(&tcpSoap);
 //默认情况下是TCP模式
 tcpSoap.port = 80;
 tcpSoap.bind_flags = SO_REUSEADDR;
 //tcpSoap.accept_timeout = tcpSoap.recv_timeout = tcpSoap.send_timeout = 5;
 soap_set_namespaces(&tcpSoap, namespaces);

 SOAP_SOCKET localSocket = SoapBind(&tcpSoap, ONVIF_TCP_IP, false);
 if (!soap_valid_socket(localSocket)) {
  printf("tcpsocket SoapBind failed!\n");
  soap_print_fault(&tcpSoap, stderr);
  exit(1);
 }

 SOAP_SOCKET clientSocket = SOAP_INVALID_SOCKET;
 while (1) {

  printf("socket connect %d\n", clientSocket);
  clientSocket = soap_accept(&tcpSoap);
  if (!soap_valid_socket(clientSocket)) {
   soap_print_fault(&tcpSoap, stderr);
   exit(1);
  }


  if (soap_serve(&tcpSoap) != SOAP_OK)
  {
   soap_print_fault(&tcpSoap, stderr);
   printf("soap_print_fault\n");
  }

  printf("IP = %u.%u.%u.%u\n", ((tcpSoap.ip) >> 24) & 0xFF, ((tcpSoap.ip) >> 16) & 0xFF, ((tcpSoap.ip) >> 8) & 0xFF, (tcpSoap.ip) & 0xFF);
  soap_destroy(&tcpSoap);
  soap_end(&tcpSoap);
 }

 //分离运行时的环境
 soap_done(&tcpSoap);
}