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

时间:2015-01-29 09:26:55

标签: udp wireshark upnp icmp ssdp

我正在尝试实现设备之间的upnp级别通信..但是在获取响应消息方面面临问题。而且更多关于发送ssdp:发现多播无法接收来自设备的消息...请指导通过我对这个话题完全不熟悉

我做的先决条件:

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

2.完成与upnp架构相关的pdf

wirehark的反应:

当我发送消息时,我收到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;
 }

1 个答案:

答案 0 :(得分:0)

你M-SEARCH中的各个行需要有&#34; \ r \ n&#34;在每一行的末尾,而不只是&#34; \ n&#34;。您的系统可能只是发送&#34; \ n&#34;穿过电线。检查您为13发送的字节数,然后是10发送的数字。那就是&#34; \ r \ n&#34;。