I recently found that when I create an UDP socket, bind it to
a local address and port, then try to receive data, which I know
is being transmitted over my WIFI from a different laptop and which
should be received "immediately" as it is being transmitted every
second, I do not receive anything unless I first transmit a small
message to the laptop from which I want to receive data.
See the example code below which illustrates the process.
If anyone has seen this before, knows why this is so and knows
a solution I would be most grateful if she/he would be willing
to share the solution with me.
// Link with ws2_32.lib
#pragma comment(lib, "Ws2_32.lib")
if(iResult != 0)
wprintf(L"bind failed with error %d\n", WSAGetLastError());
// must first send to the host from which I should
// be receiving data in order to actually receive
// the data being transmitted from that computer...
RecvAddr.sin_addr.s_addr = htonl(INADDR_ANY);
Remove the htonl(), you're not binding to the address (any) that you think you're specifying.
RecvSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
Should be: RecvSocket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
But it won't change your app behaviour as PF_INET and AF_INET have the same value.
SendTo(RecvSocket, cMessage, strlen(cMessage), 5000, _T("192.168.1.11"), 0);
You need to specify the address as a sockaddr_in (poiniter and size).
What is that 5000? The flag should be zero.