UPnP:发送发送消息但未收到响应

时间:2017-09-16 09:47:02

标签: c++ c sockets udp upnp

我正在尝试获取可用的UPnP服务列表。我发送了发现消息,但我无法得到任何回复。 192.168.1.228是发送发现消息的计算机的IP。我也试过127.0.0.1和192.168.0.1,但我仍然没有得到回复。这是我正在使用的代码:

    std::vector<std::string> UPnPDiscovery::discoverDevices(const std::string &searchTarget)
{
    // Structs needed
    struct in_addr localInterface;
    struct sockaddr_in groupSock;
    struct sockaddr_in localSock;
    struct ip_mreq group;

    std::vector<std::string> devices; // devices found

    int udpSocket;

    // Create the Socket
    udpSocket = socket(AF_INET, SOCK_DGRAM, 0);

    // Enable SO_REUSEADDR to allow multiple instances of this application to receive copies of the multicast datagrams.
    int reuse = 1;
    setsockopt(udpSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse));

    // Initialize the group sockaddr structure with a group address of 239.255.255.250 and port 1900.
    memset((char *) &groupSock, 0, sizeof(groupSock));
    groupSock.sin_family = AF_INET;
    groupSock.sin_addr.s_addr = inet_addr("239.255.255.250");
    groupSock.sin_port = htons(1900);

    // Disable loopback so you do not receive your own datagrams.
    char loopch = 0;
    setsockopt(udpSocket, IPPROTO_IP, IP_MULTICAST_LOOP, (char *)&loopch, sizeof(loopch));

    // Set local interface for outbound multicast datagrams. The IP address specified must be associated with a local, multicast capable interface.
    localInterface.s_addr = inet_addr("192.168.1.228");

    setsockopt(udpSocket, IPPROTO_IP, IP_MULTICAST_IF, (char *)&localInterface, sizeof(localInterface));

    // Bind to the proper port number with the IP address specified as INADDR_ANY.
    memset((char *) &localSock, 0, sizeof(localSock));
    localSock.sin_family = AF_INET;
    localSock.sin_port = htons(33589);
    localSock.sin_addr.s_addr = INADDR_ANY;
    bind(udpSocket, (struct sockaddr*)&localSock, sizeof(localSock));

    // Join the multicast group on the local interface. Note that this IP_ADD_MEMBERSHIP option must be called for each local interface over which the multicast datagrams are to be received.
    group.imr_multiaddr.s_addr = inet_addr("239.255.255.250");
    group.imr_interface.s_addr = inet_addr("192.168.1.228");
    setsockopt(udpSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&group, sizeof(group));

    char buff[]= "M-SEARCH * HTTP/1.1\r\n"\
    "HOST: 239.255.255.250:1900\r\n"\
    "MAN: \"ssdp:discover\"\r\n"\
    "MX: 3\r\n"\
    "ST: udap:rootservice\r\n"\
    "USER-AGENT: RTLINUX/5.0 UDAP/2.0 printer/4\r\n\r\n";

    int  ret1 = sendto(udpSocket, buff, strlen(buff), 0, (struct sockaddr*)&groupSock, sizeof groupSock);

    char discovery_response_buffer[1024] = {};
    struct sockaddr_in si_other;
    socklen_t slen = sizeof(si_other);
    while(true){

    recvfrom(udpSocket, discovery_response_buffer, 1024, 0, (struct sockaddr *) &si_other, &slen) < 0) { //TODO: edw na valw recv timeout
            perror("recvfrom - device discovery";
            break;
        }
    }
    if (close(udpSocket) < 0) {
        perror("close");
    }
    return devices;
}

你能帮我找出我在这里做错了什么吗?谢谢

0 个答案:

没有答案