I have a program that sends broadcast udp messages which runs on a windows 7 computer. I have another program which receives these udp messages. When the receiving program is run on a Windows 7 computer all is well. It receives the broadcast message correctly.

However when the same receive .exe program is run on a Windows XP computer it receives each message twice. The program below (from the code published from MSDN) demonstrates the problem. If it is run in transmit mode on one computer (XP or Win 7) and a Win7 computer runs it in receive mode then it shows once what was sent. If an XP computer runs it in receive mode then it shows twice what was sent.

Does any guru have any ideas about this please?

Code:
#define WIN32_LEAN_AND_MEAN

#include <winsock2.h>
#include <Ws2tcpip.h>
#include <stdio.h>

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

int udpRec()
{
int iResult = 0;

WSADATA wsaData;

SOCKET RecvSocket;
sockaddr_in RecvAddr;

unsigned short Port = 27015;

char RecvBuf[1024];
int BufLen = 1024;

sockaddr_in SenderAddr;
int SenderAddrSize = sizeof (SenderAddr);

	//-----------------------------------------------
	// Initialize Winsock
	iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
	if (iResult != NO_ERROR) {
		printf("WSAStartup failed with error %d\n", iResult);
		return 1;
	}

	//-----------------------------------------------
	// Create a receiver socket to receive datagrams
	RecvSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
	if (RecvSocket == INVALID_SOCKET) {
		printf("socket failed with error %d\n", WSAGetLastError());
		return 1;
	}

	//-----------------------------------------------
	// Bind the socket to any address and the specified port.
	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) {
		printf("bind failed with error %d\n", WSAGetLastError());
		return 1;
	}

	for (; ;) {

		//-----------------------------------------------
		// Call the recvfrom function to receive datagrams
		// on the bound socket.
		printf("Receiving datagrams...\n");
		iResult = recvfrom(RecvSocket, RecvBuf, BufLen, 0, (SOCKADDR *) & SenderAddr, &SenderAddrSize);
		if (iResult == SOCKET_ERROR) {
			printf("recvfrom failed with error %d\n", WSAGetLastError());
		} else {
			RecvBuf[iResult] = 0;
			printf("%s\n", RecvBuf);
		}
	}
 
	//-----------------------------------------------
	// Close the socket when finished receiving datagrams
	printf("Finished receiving. Closing socket.\n");
	iResult = closesocket(RecvSocket);
	if (iResult == SOCKET_ERROR) {
		printf("closesocket failed with error %d\n", WSAGetLastError());
		return 1;
	}

	//-----------------------------------------------
	// Clean up and exit.
	printf("Exiting.\n");
	WSACleanup();
	return 0;
}

int udpSend()
{
int iResult;
WSADATA wsaData;

SOCKET SendSocket = INVALID_SOCKET;
sockaddr_in RecvAddr;

unsigned short Port = 27015;

char SendBuf[1024];

	strcpy(SendBuf, "qwertyuiopasdfghjklzxcvbnm");

int BufLen = strlen(SendBuf);

	//----------------------
	// Initialize Winsock
	iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
	if (iResult != NO_ERROR) {
		printf("WSAStartup failed with error: %d\n", iResult);
		return 1;
	}

	//---------------------------------------------
	// Create a socket for sending data
	SendSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
	if (SendSocket == INVALID_SOCKET) {
		printf("socket failed with error: %ld\n", WSAGetLastError());
		WSACleanup();
		return 1;
	}

	BOOL bBroadcast = TRUE;
	setsockopt(SendSocket, SOL_SOCKET, SO_BROADCAST, (char *)&bBroadcast, sizeof(BOOL));

	//---------------------------------------------
	// Set up the RecvAddr structure with the IP address of
	// the receiver (in this example case "192.168.1.1")
	// and the specified port number.
	RecvAddr.sin_family = AF_INET;
	RecvAddr.sin_port = htons(Port);
	RecvAddr.sin_addr.s_addr = INADDR_BROADCAST;

	//---------------------------------------------
	// Send a datagram to the receiver
	printf("Sending a datagram to the receiver...\n");
	iResult = sendto(SendSocket, SendBuf, BufLen, 0, (SOCKADDR *) &RecvAddr, sizeof (RecvAddr));
	if (iResult == SOCKET_ERROR) {
		printf("sendto failed with error: %d\n", WSAGetLastError());
		closesocket(SendSocket);
		WSACleanup();
		return 1;
	}

	//---------------------------------------------
	// When the application is finished sending, close the socket.
	printf("Finished sending. Closing socket.\n");
	iResult = closesocket(SendSocket);
	if (iResult == SOCKET_ERROR) {
		printf("closesocket failed with error: %d\n", WSAGetLastError());
		WSACleanup();
		return 1;
	}

	//---------------------------------------------
	// Clean up and quit.
	printf("Exiting.\n");
	WSACleanup();
	return 0;
}


//use from the command line
//no args is send mode
//with args is receive mode
int main(int argc, char *argv[])
{
	if (argc > 1)
		udpRec();
	else
		udpSend();

	return 0;
}