Q:

C++ Winsock UDP sendto succeeds but I don't see the data being sent

I am working with UDP sockets over an ethernet interface between my workstation (192.168.0.1) and my WinCE6 device (192.168.0.100).
From my workstation I can send UDP packets toward my WinCE device where I have a receiving socket set up and properly receiving data on port 9002; the device then properly echoes back data to my workstation.
I want my device to answer to my workstation on a specific port: 9001.
This is the server running into my device, it is the same as this but I modified the socket bind to the address of the specific interface and I changed the PORT define:
#define BUFLEN 512  //Max length of buffer
#define PORT 9002   //The port on which to listen for incoming data

void test_udp( void )
{
SOCKET s;
struct sockaddr_in server, si_other;
int slen , recv_len;
char buf[BUFLEN];
WSADATA wsa;

slen = sizeof(si_other) ;

//Initialise winsock
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
{
    return;
}

//Create a socket
if((s = socket(AF_INET , SOCK_DGRAM , 0 )) == INVALID_SOCKET)
{
    printf( "Could not create socket : %d" , WSAGetLastError());
}

//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl( 0xC0A80064 );
server.sin_port = htons( PORT );

//Bind
if( bind(s ,(struct sockaddr *)&server , sizeof(server)) == SOCKET_ERROR)
{
    return;
}

//keep listening for data
while(1)
{
    //clear the buffer by filling null, it might have previously received data
    memset(buf,'\0', BUFLEN);

    //try to receive some data, this is a blocking call
    if ((recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen)) == SOCKET_ERROR)
    {
        return;
    }

    // *** CHANGE THE SEND PORT
    si_other.sin_port = htons( 9001 );

    //now reply the client with the same data
    if (sendto(s, buf, recv_len, 0, (struct sockaddr*) &si_other, slen) == SOCKET_ERROR)
    {
        return;
    }
}

closesocket(s);
WSACleanup();

return;
}

When I added the line below the // *** CHANGE THE SEND PORT comment to try to send the data back to port 9001, I am getting an appropriate return value for sendto which matches the number of bytes I expect to have been sent, but I don't see the data coming on my workstation (listening on 9001).
I have been banging my head against this and I cant see anything wrong. In the modified example,  Any help would be greatly appreciated.
UPDATE: A firewall on my development PC was at fault. It allowed traffic back on the same port my PC was sending on, but blocked traffic on any other port. Resolving that allowed the the traffic to be sent without code changes. I had disabled the firewall previously, but did not realize that the OS had flagged the ethernet as a "public network", which still had the firewall enabled.

A:

If, as you said, you are not getting a SOCKET_ERROR from the sendto and you are seeing an appropriate return value, this means that your OS was able to find a route toward the destination IP and was able to buffer your send request.
To change the send port it's ok if you want to answer to a different port with respect to the one from which you received data but note that this is not a typical case; the si_other struct in fact will contain the source address after the recvfrom and so the source port too. So keep your code as it is on your device side.
If you are not seeing data flowing to your workstation probably, in a simple networking context as your development environment, the problem is on the workstation: it is not receiving data.
I agree with @Daniel that you have to investigate with WireShark to see what's happening to packets.
Hope this helps

