UDP WinSock - Not Receiving Broadcast Packets
<p>I'm testing a simple socket setup in which a server listens on a specified port, and a client sends a broadcast packet which should be received by that server.</p>

<p>This setup works fine when sending messages directly (i.e. not broadcasting), but when broadcasting the packet is never received on the server.</p>

<p>Some of the code (trimmed down with error checking removed, for simplicity):</p>

<pre><code>// Client (broadcast sender)

// Create and bind the client socket
clientSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

sockaddr_in sockAddr;
sockAddr.sin_family = AF_INET;
sockAddr.sin_port = htons(5678);
sockAddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);

bind(clientSocket, (sockaddr*)(&amp;sockAddr), sizeof(sockAddr));

u_long uMode = 1;
ioctlsocket(clientSocket, FIONBIO, &amp;uMode);

char broadcast = 1;
setsockopt(clientSocket, SOL_SOCKET, SO_BROADCAST, &amp;broadcast, sizeof(broadcast));

// ...

// Send the packet
sockaddr_in sockAddress;
sockAddress.sin_family = AF_INET;
sockAddress.sin_addr.S_un.S_addr = htonl(INADDR_BROADCAST);
sockAddress.sin_port = htons(5679);

char const* pPacket = "Test";
size_t uPacketSize = strlen(pPacket) + 1;

sendto(clientSocket, pPacket, (int)uPacketSize, 0, (sockaddr*)&amp;sockAddress, sizeof(sockAddress));
</code></pre>

<p>-</p>

<pre><code>// Server (listener)

// Create and bind the server socket
serverSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

sockaddr_in sockAddr;
sockAddr.sin_family = AF_INET;
sockAddr.sin_port = htons(5679);
sockAddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);

bind(serverSocket, (sockaddr*)(&amp;sockAddr), sizeof(sockAddr));

u_long uMode = 1;
ioctlsocket(serverSocket, FIONBIO, &amp;uMode);

// ...

char pBuffer[1024];
while (true)
{
    int iRecvSize = recv(serverSocket, pBuffer, 1024, 0);
    if (iRecvSize)
    {
        printf("Received packet\n");
    }
}
</code></pre>