Problem with simple Arduino UDP to Windows 7/64 using C++

I am having a problem with a simple Arduino to Windows 7/64 UDP.

Things I know:

  1. Windows can ping Arduino (10.0.0.128 in my case)
  2. Arduino can UDP to Windows Processing program succesfully
  3. A Ethernet sniffer program (WireShark) has captured several sample UDP packets from the Arduino
  4. Loop back with a UDP client and a UDP server on PC box works correctly.
  5. My PC is 10.0.0.7 and I am trying to use port 0x7777 on PC and 0x7778 on Arduino

The Windows program hangs (never returns) on the recvfrom call in the C++ Server.
Sample server code is from http://msdn.microsoft.com/en-us/library/windows/desktop/ms740120(v=vs.85).aspx
There are multiple sites on arduino.cc that have sample Arduino UDP clients.

I have tried several variants on the C++ side (binding to a specific IP address instead of INADDR_ANY for example) but the server program on the PC still does not return from the recvfrom call.

I have been doing Linux/Windows UDP for over 20 years but this has me stumped.

Surely someone has written Windows 7’64 server program to receive a UDP Arduino packet.

It doesn’t even have to be C++.

Help will be appreciated and thanks to any efforts in advance.


ARDUINO CODE


#include <SPI.h> // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008

byte myMAC = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

IPAddress myIP(10,0,0,128);
IPAddress targetIP(10,0,0,7);
unsigned int myPort = 0x7778;
unsigned int targetPort = 0x7777;
int msgNumber = 0;

char HelloMsg = "UPD Hello from Arduino - ";

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

void printIPAddr(char * label,IPAddress IPAddr)
{
Serial.print(label);
Serial.print(IPAddr[0]);
for (int iPosition=1; iPosition<4; iPosition++)
{
Serial.print(".");
Serial.print(IPAddr[iPosition]);
} // for (int iPosition=1; iPosition<4, iPosition++)
Serial.println("’ done");
}

void setup()
{
// start the Ethernet and UDP:
Ethernet.begin(myMAC,myIP);
Udp.begin(myPort);
Serial.begin(9600);
printIPAddr(“myAddress '”,myIP);
printIPAddr(“targetAddress '”,targetIP);
Serial.print(“myPort 0X”);
Serial.println(myPort,HEX);
Serial.print(“targetPort 0X”);
Serial.println(targetPort,HEX);
}
void loop()
{
msgNumber++;
Serial.print("Enter loop - ");
Serial.println(msgNumber,DEC);
Udp.beginPacket(targetIP,targetPort);
Serial.println(“Completed Udp.beginPacket”);
Udp.print(HelloMsg);
Udp.print(msgNumber,DEC);
Serial.println(“Into endPacker”);
Udp.endPacket();
Serial.println(“Completed Udp.endPacket”);
delay(1000);
}


PROCESSING server running correctly on Windows 7


import hypermedia.net.*;

UDP udp;

void setup()
{
println(“Setup”);
udp = new UDP(this, 0x7777);
udp.listen(true);
}
void draw() {}

void receive (byte data)
{
println(“Into receive”);
println(data.length);
for (int iCharPosition = 0; iCharPosition < data.length; iCharPosition++)
{
print(char(data[iCharPosition]));
} // for (int iCharPosition = 0; iCharPosition < data.length; iCharPosition++)
println();
}


Windows C++ server that hangs on recvfrom


#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

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

#pragma comment(lib,“ws2_32.lib”)

#define BUFFER_SIZE 4096

#define ARGV_PROGRAM_NAME 0
#define ARGV_MY_PORT 1
#define ARGV_COUNT 2

int main(int argc, char **argv)
{
WSADATA w; // Used to open windows connection
int wsaResult; // Status of initialization of Windock
unsigned short server_port; // Port number to use
int client_length; // Length of client struct
int iBytesRead; // Bytes received from client
sockaddr_in server; // Information about the server
sockaddr_in client; // Information about the client
char buffer[BUFFER_SIZE]; // Where to store received data
SOCKET listenSocket; // Server socket

// Check argument count
if (argc != ARGV_COUNT)
{
printf(“Incorrect argument count\n”);
return 1;
}

// Get my port number
if (sscanf_s(argv[ARGV_MY_PORT], “%x”, &server_port) != 1)
{
printf(“Conversion error for port\n”);
return 1;
}

// Open windows connection
if ((wsaResult = WSAStartup(0x202, &w)) != 0)
{
printf( “Could not open Windows connection\n”);
return 1;
}

// Open a datagram socket
if ((listenSocket = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET)
{
printf( “Could not create socket - %d\n”, WSAGetLastError());
WSACleanup();
return 1;
}

// Build server struct
memset((void *)&server, 0, sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(server_port);
server.sin_addr.s_addr = htonl(INADDR_ANY);

// Bind address to socket
if (bind(listenSocket, (SOCKADDR *)&server, sizeof(server)) != 0)
{
printf( “Could not bind name to socket - %d\n”, WSAGetLastError());
WSACleanup();
return 1;
}

client_length = (int)sizeof(client);

// Receive bytes from client
printf( “Call recvfrom\n”);
iBytesRead = recvfrom(listenSocket, buffer, BUFFER_SIZE, 0,(SOCKADDR *)&client, &client_length);
printf( “Back from recvfrom\n”);

if (iBytesRead == SOCKET_ERROR)
{
printf(“Could not receive datagram %d\n”, WSAGetLastError());
WSACleanup();
return 1;
}

printf( “Bytes read from client = %d\n”, iBytesRead);
printf( “Message from client ‘%s’\n”, buffer);
printf( “Client address %u.%u.%u.%u\n”, (unsigned char)client.sin_addr.S_un.S_un_b.s_b1,
(unsigned char)client.sin_addr.S_un.S_un_b.s_b2,
(unsigned char)client.sin_addr.S_un.S_un_b.s_b3,
(unsigned char)client.sin_addr.S_un.S_un_b.s_b4);
printf( “Socket reports port %d %04X\n”,ntohs(client.sin_port),ntohs(client.sin_port));

WSACleanup();
return 0;
}


Ethernet Sniffer Capture (just because I have it)


Added as an attachment because I am having trouble pasting and image in the message

I finally gave up on using Microsoft C++.net - I switched to JAVA and the sun came up and shone on my face.