我正试图在C++中执行菲律宾国家警察,但我发现有几处来源,但没有工作。 我发现这项工作(。 http://www.codeproject.com/KB/IP/upnplib.aspx,但用于www.NET,因此,我决定向网络发出信息,以了解该守则正在做些什么,然后与书状相同。
Here are the results (full size: https://i.stack.imgur.com/eLoHK.jpg):
这表明,包装单只看坏,似乎所有东西都是一样的,但我不知道如何控制(我的代码和发现器.net.exe都在同一个网络的计算机上测试)。
我的守则如下:
#define upnp_broadcast_ip "239.255.255.250"
#define upnp_broadcast_port 1900
#define upnp_search_request "M-SEARCH * HTTP/1.1
"
"Host:239.255.255.250:1900
"
"ST:upnp:rootdevice
"
"Man:"ssdp:discover"
"
"MX:3
"
"
"
WSAStartup(MAKEWORD(2, 2), &WsaData);
BOOL discover( )
{
SOCKET ConnectSocket;
struct sockaddr_in Addr;
char Buffer[1450];
int t = 0,
iResult = 0,
TrueLen = sizeof(bool);
bool True = true;
ulong One = 1;
// Open datagram socket
ConnectSocket = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
// Clear out struct
memset( &Addr, 0, sizeof(Addr) );
// Specify the address family, IP address, and port
Addr.sin_family = AF_INET;
Addr.sin_port = htons( upnp_broadcast_port );
Addr.sin_addr.s_addr = inet_addr( upnp_broadcast_ip );
iResult = setsockopt( ConnectSocket, SOL_SOCKET, SO_BROADCAST, (char*)&True, TrueLen ); // Not sure what is this for
// Transmit data
int sent = sendto( ConnectSocket, upnp_search_request, strlen(upnp_search_request), 0, (struct sockaddr*)&Addr, sizeof(Addr) );
// Try to receive data 10 times
for( t = 0; t < 10; t++ )
{
ioctlsocket( ConnectSocket, FIONBIO, &One );
// Clear out buffer
memset( &Buffer, 0, sizeof(Buffer) );
int length = sizeof(Addr);
// Receive data
iResult = recvfrom( ConnectSocket, Buffer, (sizeof(Buffer) - 1), 0, (struct sockaddr*)&Addr, &length );
if( iResult == SOCKET_ERROR)
{
Sleep( 1000 );
continue;
} else {
// Do stuff with received data
}
}
closesocket( ConnectSocket );
return FALSE;
}
我删除了所有AGet LastError(WAGet LastError()的错误核对,使密码更容易阅读,所有事情都经过了罚款,直到从中删除,这总是回报-1和tre(WSAGet LastError())的印刷“不知错误”。
我希望有人能够引导我朝着正确方向前进,我是最后两天努力完成这项工作。