开发者

UPnP discovery in c++?

I am trying to implement UpNp in a c++ application, I found this code with google:

BOOL upnp_discover( upnp_device_t* Device, ushort Tries )
{
    SOCKET              Sock;
    struct sockaddr_in  Addr;
    char                Buffer[1450],
                       *Begin   = NULL,
                       *End     = NULL;
    int                 i       = 0,
                        t       = 0,
                        Ret     = 0,
                        TrueLen = sizeof(bool);
    bool                True    = true;
    ulong               One     = 1,
                        Zero    = 0;
    Sock = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
    memset( &Addr, 0, sizeof(Addr) );
    Addr.sin_family      = AF_INET;
    Addr.sin_port        = htons( upnp_broadcast_port );
    Addr.sin_addr.s_addr = inet_addr( upnp_broadcast_ip );
    Ret = setsockopt( Sock, SOL_SOCKET, SO_BROADCAST, (char*)&True, TrueLen );
    for( i = 0; i < Tries; ++i )
    {
        memset( &Buffer, 0, sizeof(Buffer) );
        strcpy( Buffer, upnp_search_request );
        sendto( Sock, Buffer, strlen(Buffer), 0, (struct sockaddr*)&Addr, sizeof(Addr) );
        for( t = 0; t < 10; ++t )
        {
            ioctlsocket( Sock, FIONBIO, &One );
            memset( &Buffer, 0, sizeof(Buffer) );
            Ret = recvfrom( Sock, Buffer, (sizeof(Buffer) - 1), 0, NULL, NULL );
            if( Ret == SO开发者_运维问答CKET_ERROR)
            {
                Sleep( 1000 );
                continue;
            } else {
                Begin = strstr( Buffer, "http://" );
                if( Begin != NULL )
                {
                    End = strchr( Begin, '\r' );
                    if( End != NULL )
                    {
                        *End = '\0';
                        strncpy( Device->Location, Begin, (sizeof(Device->Location) - 1) );
                        upnp_parse_url( Device );
                        closesocket( Sock );
                        return TRUE;
                    }
                }
            }
        }
        ioctlsocket( Sock, FIONBIO, &Zero );
    }
    closesocket( Sock );
    return FALSE;
}

But it always returns false, and im behind a router with UPnP enabled. I've found a few other sources on google but they are too dependents of the rest of the application they belong and since I am kinda new at c++ I can barely make sense of them.

Does someone have a simple example of using UpNP in c++?


EDIT:

Debugging shows that:

Ret = recvfrom( Sock, Buffer, (sizeof(Buffer) - 1), 0, NULL, NULL );

Is always -1 (SOCKET_ERROR), does someone knows why this happens?


I wonder if you would prefer to take advantage of existing UPnP APIs in Windows.

Purpose

The UPnP™ framework enables dynamic networking of intelligent appliances, wireless devices, and PCs. There are two APIs for working with UPnP-certified devices:

The Control Point API, which consists of a set of COM interfaces used to find and control devices.
The Device Host API, which consists of a set of COM interfaces used to implement devices that are hosted by a computer.


There's also a free UPnP library tailored to port forwarding on home NATs here: http://miniupnp.free.fr/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜