Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Winsock recvfrom, message length

Posted on 2004-04-17
Medium Priority
Last Modified: 2013-12-03
I'm porting a unix socket (UDP) app to windows. On unix, recvfrom will return the length of the message (even if it didn't fit in the buffer i passed to the function). I need to obtain the length of the incoming message, but this seems to be somehow messed up in the MS API, which returns -1 on failure and never seems to be able to give me the message length. On success, of course it gives me the actual number of bytes I got. However this is rather uninteresting since by then I've obviously already allocated a proper buffer. I need to know ahead of time what size buffer is needed to hold the message. I'd be very happy if someone could tell me what function I need to use to do this on windows ASAP.
Question by:limestar
  • 2
  • 2
  • 2

Accepted Solution

aib_42 earned 2000 total points
ID: 10858120
You might want to take a look at the ioctlsocket() function. Particularly, the "FIONREAD" command, which you would probably implement as:
if (ioctlsocket(mysocket, FIONREAD, &nextreadsize)) { error } where nextreadsize is an unsigned long parameter, which will hopefully hold the length of the buffer you are going to read.

This is directly from MSDN at [ http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/ioctlsocket_2.asp ]:

Use to determine the amount of data pending in the network's input buffer that can be read from socket s. The argp parameter points to an unsigned long value in which ioctlsocket stores the result. FIONREAD returns the amount of data that can be read in a single call to the recv function, which may not be the same as the total amount of data queued on the socket. If s is message oriented (for example, type SOCK_DGRAM), FIONREAD still returns the amount of pending data in the network buffer, however, the amount that can actually be read in a single call to the recv function is limited to the data size written in the send or sendto function call.

Expert Comment

ID: 10861790

Or use MSG_PEEK flag with recvfrom() call. This would return the number of bytes pending to recv.

But, I doubt if your original question is valid. This is what I read on recvfrom manual (on Solaris)

     These calls return the number of bytes received, or -1 if an
     error occurred.

It says recvfrom() returns the number of bytes received, not the number of bytes to receive.
Logically, that is the way it should work. recvfrom() call should give you the number of bytes received.
- Just like read() fread() etc.

Check out the code once again and see is that is correct .


Expert Comment

ID: 10861923
Ah yeah, forgot to mention MSG_PEEK...

It is funny how the text ".....and the function returns the number of bytes currently pending to receive." (in the description of the MSG_PEEK flag) only appears in the more recent versions of the WIN32 SDK Help.
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.


Author Comment

ID: 10862568
Well, if you'd actually have read the original question except jumping madly for the points, you'd have seen that I stated clearly that thats just what we do on unix-based systems. However, MS has f**ked up their port of the sockets lib, so that recvfrom() returns SOCKET_ERROR instead of something proper (regardless of flags passed to it).

Anyway, thanks to aib_42 for the suggestion, I'll try it out later today and accept the answer if it works out.

Expert Comment

ID: 10862759

>> Well, if you'd actually have read the original question except jumping madly for the points,
>> you'd have seen that I stated clearly that thats just
>> what we do on unix-based systems.

duh!  I guess I knew that !!
May be you should do a little more eyeballing on others commenst before you add in more "stars" into your comments.

What I was trying to say was I am not sure if unix returns the number of bytes in the buffer - not the bytes actually read
and thats why I pasted the "man" page.

FYI 1 : This group is "Programming_Platforms/Win_Prog" meaning people will assume all questions are realted to windows.

FYI 2 : most of the socket library calls are similar in winsows and unix. So, when I said MSG_PEEK, it applies to Win32 as well.

FYI 3: People will post comments to all interesting questions even if there are only 50 points. ( I can see that from your own profile :)

FYI 4: There are people cursing MS because they know the in and out of the Windows system.
I have seen other people cursing too - may be just for the sake of cursing. :)


Author Comment

ID: 10862972
Right. My point was just that the socket library calls *look* similar on unix and windows. However, after some time programming winsock2, you'll gather rather quickly that it doesn't behave right.

1. Of course. This question is strictly windows. It works fine on unix, and I want to know how to get a certain functionallity out of the windows socket library.

2. They look the same. However, they don't work the same. Like I said, MSG_PEEK does NOT work the correct way in Win32, because recvfrom() will *still* return SOCKET_ERROR instead of the actual amount of bytes in the buffer. I guess I should've mentioned in the original code that the flag to the call was originally MSG_PEEK. This doesn't work however. MSDN is rather unclear on the subject, but after reading it several times you'll actually come to the conclusion that one possible way of interpreting it is that setting MSG_PEEK only returns the number of bytes if they were actually copied.

3. Of course. But you must also agree that alot of people will point whatever BS into a 500 pt question hoping to get the points even though they didn't even nearly answer the question....

4. I've done enough Win32 programming to curse MS with a good reason. Try the error conditions of the LoadLibrary() calls for instance. What happens if you run it on an invalid file? Error code? Nope, message box saying the user should check their installation diskette. The messed up API they call Win32 makes my work a big pain.

Check it out if you're too lazy to write the test app yourself. I find it rather entertaining:

Btw, the points of this question were not set because I beleived it difficult - it was set because I needed the solution ASAP. Sorry if I came across the wrong way.

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question