strange error with recvfrom

Started by
6 comments, last by Fistandantilus 20 years, 11 months ago
I´m getting a strange error when using recvfrom. I´ve used u_long on = 1; ioctlsocket(skServer,FIONBIO,&on); in order to make the socket non blocking. then I have a loop while(...) { if(recvfrom(skServer,...) == SOCKET_ERROR) PrintErrorMessage(GetLastError()); } first time recvfrom runs , it goes ok. then second time , in gives SOCKET_ERROR , when I enter the PrintErrorMessage that is mainly a switch(ErrorCode) with all errors in the cases , the error is not any of the possible errors. What is going on? Please help!... Please?
Advertisement
This code is not enough to diagnose your problem... Post more

"I''ll be Bach!" (c) Johann Sebastian Schwarzenegger
"I'll be Bach!" (c) Johann Sebastian Schwarzenegger
> PrintErrorMessage(GetLastError());

Use WSAGetLastError() instead.

-cb
The error you will get on an unsuccessful recvfrom() call will be -1 (i.e. the generic SOCKET_ERROR).

As cbenoi1 said, use WSAGetLastError() rather than GetLastError() to resolve socket errors, and feed that to your PrintErrorMessage routine.

There are quite a few possible errors that call could return. My guess, since it ran fine once then crashed the second time, would be you're getting a WSAEMSGSIZE error, which just means the message was too large to fit into the buffer.

BTW, using a switch statement for your error printing routine is not the best idea. This bit of code (which I didn't write, I stole it off the web somewhere ) will give you better results, since it automatically will pick up error strings. It will handle both input values obtained from GetLastError() and WSAGetLastError(). Just be careful to pass a 1024 sized buffer in String, or you'll end up with AV errors.

void SomeClass::GetErrorString(int ErrorCode, char *String)    {    DWORD fOk;    HMODULE hDll;    // Get the error code's textual description    fOk = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL,                ErrorCode, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),(LPTSTR) &String, 1024, NULL);    if (fOk == 0)        {        // Is it a network-related error?        hDll = LoadLibraryEx(TEXT("netmsg.dll"), NULL,DONT_RESOLVE_DLL_REFERENCES);        if (hDll != NULL)            {            FormatMessage(FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_FROM_SYSTEM,                      hDll, ErrorCode, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), (LPTSTR) &String, 1024, NULL);            FreeLibrary(hDll);            }        else            strcpy (String, "Unkown Error: Error number not found.");        }    } 


HTH

Ron

[edited by - RonHiler on May 8, 2003 10:26:31 AM]
Creation is an act of sheer will
> There are quite a few possible
> errors that call could return.

True. My guess is that the second time around the error will be WSAEWOULDBLOCK. This is consistent with a tight loop over a *non-blocking* socket and is not an error per se; it''s an indication that there is nothing to read anymore for now.

-cb
> My guess is that the second time around the error will be WSAEWOULDBLOCK

Oh, yeah. That''s another good possiblity.
Creation is an act of sheer will
Thanks for the replies.

cbenoi1 you are right. I´m receiving WSAEWOULDBLOCK in the second recvfrom. Strange thing this being considered an error.



Well, a non-error return would obviously be wrong, as that would indicate that a packet has been received, which is not the case (note: a return value of zero indicates a zero-length packet has been received). So an "error" return is a sane solution, especially if you consider that most blocking syscalls can return with an EINTR error when a signal was raised during the wait (that isn't strictly an error either).

cu,
Prefect

[edited by - Prefect on May 8, 2003 1:30:17 PM]
Widelands - laid back, free software strategy

This topic is closed to new replies.

Advertisement