如何在 upnp 设备之间执行 ssdp 通信

how to perform ssdp communicationbetween upnp devices

我正在尝试实现设备之间的 upnp 级通信。但是在获取响应消息时遇到问题。发送 ssdp:discovery 多播无法从设备接收消息。 .. 请引导我完成我对这个话题的全新认识

先决条件由我完成:

1.able 发送 M-Search ..并在网络上通知消息 ..并已通过 wireshark 确认

2.gone透upnp架构相关pdf

在 wireshark 中得到响应:

每当我发送消息时,我都会收到 icmp 错误消息,指出目的地无法到达..

<客户端代码>是第一个,第二个是暂时我只是在本地主机上发送数据

   #include <arpa/inet.h>
   #include <netinet/in.h>
   #include <stdio.h>
   #include <sys/types.h>
   #include <sys/socket.h>
   #include <unistd.h>



   #define SRV_IP "127.0.0.1"
   /* diep(), #includes and #defines like in the server */

  #define BUFLEN 512
   #define NPACK 10
  #define PORT 1900

void diep(char *s)
  {
    perror(s);
    exit(1);
  }

   int main(void)
   {
     struct sockaddr_in si_other;
     int s, i, slen=sizeof(si_other);
     char buf[BUFLEN];

    if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
      diep("socket");

    memset((char *) &si_other, 0, sizeof(si_other));
    si_other.sin_family = AF_INET;
    si_other.sin_port = htons(PORT);
    if (inet_aton(SRV_IP, &si_other.sin_addr)==0) {
      fprintf(stderr, "inet_aton() failed\n");
      exit(1);
    }

    for (i=0; i<NPACK; i++) {
      printf("Sending packet %d\n", i);
      sprintf(buf, "\n");
      if (sendto(s, buf, BUFLEN, 0, &si_other, slen)==-1)
        diep("sendto()");
    }

    close(s);
    return 0;
  }







  #include <arpa/inet.h>
   #include <netinet/in.h>
   #include <stdio.h>
   #include <sys/types.h>
   #include <sys/socket.h>
   #include <unistd.h>

   #define BUFLEN 512
   #define NPACK 10
  #define PORT 1900

  void diep(char *s)
  {
    perror(s);
    exit(1);
  }

  int main(void)
  {
    struct sockaddr_in si_me, si_other;
    int s, i, slen=sizeof(si_other);
    char buf[BUFLEN];

    if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
      diep("socket");
        memset((char *) &si_me, 0, sizeof(si_me));
    si_me.sin_family = AF_INET;
    si_me.sin_port = htons(PORT);
    si_me.sin_addr.s_addr = htonl(INADDR_ANY);
    if (bind(s, &si_me, sizeof(si_me))==-1)
        diep("bind");

    for (i=0; i<NPACK; i++) {
      if (recvfrom(s, buf, BUFLEN, 0, &si_other, &slen)==-1)
        diep("recvfrom()");
      printf("Received packet from %s:%d\nData: %s\n\n", 
             inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port), buf);
    }

    close(s);
    return 0;
 }

你M-SEARCH中的每一行都需要在每行的末尾有“\r\n”,而不仅仅是一个“\n”。您的系统可能只是通过网络发送“\n”。检查您发送的字节是 13 还是 10。那是“\r\n”。