UDP Socket

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.

Kind regards,
Jeroen Posch.


//
// Link with ws2_32.lib
//
#pragma comment(lib, "Ws2_32.lib")

#include "SendTo.h"

void TestUdpSocket()
{
int iResult = 0;

WSADATA wsaData;

SOCKET RecvSocket;

sockaddr_in RecvAddr;

const int BufLen = 1024;

char RecvBuf[BufLen] = {0};

sockaddr_in SenderAddr;

int SenderAddrSize = sizeof (SenderAddr);

//-----------------------------------------------
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);

if(iResult != NO_ERROR)
{
wprintf(L"WSAStartup failed with error %d\n", iResult);

return;
}

//-----------------------------------------------
// Create a receiver socket to receive datagrams

RecvSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

if(RecvSocket == INVALID_SOCKET)
{
wprintf(L"socket failed with error %d\n", WSAGetLastError());

return;
}

//-----------------------------------------------
// Bind the socket to any address and the specified port.

unsigned short Port = 5000;

RecvAddr.sin_family = AF_INET;
RecvAddr.sin_port = htons(Port);
RecvAddr.sin_addr.s_addr = htonl(INADDR_ANY);

iResult = bind(RecvSocket, (SOCKADDR *) & RecvAddr, sizeof (RecvAddr));

if(iResult != 0)
{
wprintf(L"bind failed with error %d\n", WSAGetLastError());

return;
}

//-----------------------------------------------
// 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...

char cMessage[] = "HELLO FROM JEROEN\r\n";

SendTo(RecvSocket, cMessage, strlen(cMessage), 5000, _T("192.168.1.11"), 0);

//-----------------------------------------------
// Call the recvfrom function to receive datagrams
// on the bound socket.

wprintf(L"Receiving datagrams...\n");

iResult = recvfrom(RecvSocket, RecvBuf, BufLen, 0, (SOCKADDR *) & SenderAddr, &SenderAddrSize);

if(iResult == SOCKET_ERROR)
{
wprintf(L"recvfrom failed with error %d\n", WSAGetLastError());
}

//-----------------------------------------------
// Close the socket when finished receiving datagrams

wprintf(L"Finished receiving. Closing socket.\n");

iResult = closesocket(RecvSocket);

if(iResult == SOCKET_ERROR)
{
wprintf(L"closesocket failed with error %d\n", WSAGetLastError());

return;
}

//-----------------------------------------------
// Clean up and exit.

wprintf(L"Exiting.\n");

WSACleanup();
}
Please edit to include code tags, so we can read your code.
http://www.cplusplus.com/articles/jEywvCM9/

Also, all of the code please.
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.
Last edited on
Topic archived. No new replies allowed.