Archived

This topic is now archived and is closed to further replies.

Fistandantilus

strange error with recvfrom

Recommended Posts

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?

Share this post


Link to post
Share on other sites
This code is not enough to diagnose your problem... Post more

"I''ll be Bach!" (c) Johann Sebastian Schwarzenegger

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
> 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

Share this post


Link to post
Share on other sites
> My guess is that the second time around the error will be WSAEWOULDBLOCK

Oh, yeah. That''s another good possiblity.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites