Reputation: 511
i'm trying to receive data on a UDP socket, but I cant find a way to receive anything.
I can see with wireshark that the data is actually coming to the computer, but recvfrom always timeout.
The involved code is pretty simple :
mUDPSocket = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
if( mUDPSocket == INVALID_SOCKET )
return 1;
uint32_t aTimeout = 5000;
const int lResult = setsockopt( mUDPSocket, SOL_SOCKET, SO_RCVTIMEO, ( char * )&aTimeout, sizeof( aTimeout ) );
if( SOCKET_ERROR == lResult )
return 1;
struct sockaddr_in addr = {};
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl( INADDR_ANY ); //also tried with my local ip
addr.sin_port = htons( 48640 );
if( bind( mUDPSocket, ( const sockaddr * )&addr, sizeof( addr ) ) < 0 )
return 1;
And later when trying to receive data
if(recvfrom( mUDPSocket, ( char * )aData, aSize, 0, NULL, 0 )<0) //function return -1
std::cout << WSAGetLastError() << std::endl; //10060
Im trying to do something basic, so I guess im missing something simple.
Edit : the packet is sent several time by second and the timeout is 5second, so I should receive it before the real timeout
I've attached a screen of the wireshark capture
Upvotes: 2
Views: 4025
Reputation: 597941
The only way I can see the code as shown timing out is if either:
the datagram is not arriving before aTimeout
elapses (what is the actual value of aTimeout
?)
the datagram is not using IPv4
the datagram is not being sent to an IP on the machine, or is not being sent to port 48640
the datagram is being blocked, such as by a firewall.
Upvotes: 4