I want to know the size of the next UDP datagram in the system's queue.
I found this question with a similar doubt, but using boost. The last answer (as of 2010/09/23) say something about using getsockopt
with the SO_NREAD
option in OS X, but I can't find anything about this with Windows (using Winsock).
Here I found that I can use ioctlsocket
with FIONREAD
to find out what is the size of the entire queue, but I didn't find anything about the first datagram.
So my question is: Is there a way to determine what is the size of the next UDP datagram in the queue using the sockets API? (I'm not using boost).
I want my code to look like this:
char BigBuffer[ 64 * 1024 ];
void Read( void *Buf, size_t Size ) {
size_t LengthInQueue = WhatTheSizeOfTheNextDatagram();
if( Size < LengthInQueue ) {
recvfrom( Socket, BigBuffer, 64*1024, /*...*/ );
memcpy( Buf, BigBuffer, Size );
}
else {
recvfrom( Socket, Buf, size, /*...*/ );
}
}
I left out error checking and some parameters for the sake of space and readability. I want to avoid copying to a intermediary buffer when its not needed.