Solved

Winsock recvfrom, message length

Posted on 2004-04-17
6
1,019 Views
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.
0
Comment
Question by:limestar
  • 2
  • 2
  • 2
6 Comments
 
LVL 7

Accepted Solution

by:
aib_42 earned 500 total points
Comment Utility
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 ]:

FIONREAD
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.
0
 
LVL 8

Expert Comment

by:mxjijo
Comment Utility


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)

RETURN VALUES
     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 .



0
 
LVL 7

Expert Comment

by:aib_42
Comment Utility
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.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 2

Author Comment

by:limestar
Comment Utility
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.
0
 
LVL 8

Expert Comment

by:mxjijo
Comment Utility


>> 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. :)

:)
0
 
LVL 2

Author Comment

by:limestar
Comment Utility
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:
http://www.limebird.com/~limestar/windowsfel.jpg

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.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SSIS Lookup not matching 4 49
Insert filename column as column a 18 70
Need to create an object factory 2 41
Best book to learn C++ 4 52
In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now