Note that there are some explanatory texts on larger screens.

plurals
  1. POSocket transmit data but can't receive response
    primarykey
    data
    text
    <p>I am trying to implement UpNP in C++, I found a few sources on google but none worked. I found this one working (<a href="http://www.codeproject.com/KB/IP/upnplib.aspx" rel="nofollow noreferrer">http://www.codeproject.com/KB/IP/upnplib.aspx</a>) but it's for .NET, so I decided to sniff the network to see what the code was doing and then do the same with sockets.</p> <p>Here are the results (full size: <a href="https://i.stack.imgur.com/eLoHK.jpg" rel="nofollow noreferrer">http://i.stack.imgur.com/eLoHK.jpg</a>): <img src="https://i.stack.imgur.com/eLoHK.jpg" alt="enter image description here"></p> <p>That shows me that the packet doesn't look bad, everything seems to be the same, everything but the <strong>source address</strong> of my code, which I don't know how to control (both my code and finder.net.exe are being tested on the same computer connected to the same network).</p> <p>Here's my code:</p> <pre><code>#define upnp_broadcast_ip "239.255.255.250" #define upnp_broadcast_port 1900 #define upnp_search_request "M-SEARCH * HTTP/1.1\r\n" \ "Host:239.255.255.250:1900\r\n" \ "ST:upnp:rootdevice\r\n" \ "Man:\"ssdp:discover\"\r\n" \ "MX:3\r\n" \ "\r\n" WSAStartup(MAKEWORD(2, 2), &amp;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( &amp;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*)&amp;True, TrueLen ); // Not sure what is this for // Transmit data int sent = sendto( ConnectSocket, upnp_search_request, strlen(upnp_search_request), 0, (struct sockaddr*)&amp;Addr, sizeof(Addr) ); // Try to receive data 10 times for( t = 0; t &lt; 10; t++ ) { ioctlsocket( ConnectSocket, FIONBIO, &amp;One ); // Clear out buffer memset( &amp;Buffer, 0, sizeof(Buffer) ); int length = sizeof(Addr); // Receive data iResult = recvfrom( ConnectSocket, Buffer, (sizeof(Buffer) - 1), 0, (struct sockaddr*)&amp;Addr, &amp;length ); if( iResult == SOCKET_ERROR) { Sleep( 1000 ); continue; } else { // Do stuff with received data } } closesocket( ConnectSocket ); return FALSE; } </code></pre> <p>I removed all the WSAGetLastError() error checking to make the code easier to read, everything goes fine until recvfrom, that always returns -1 and strerror(WSAGetLastError()) prints "Unknown error".</p> <p>I hope someone could guide me in the right direction, I've been the last two days trying to make this work.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload