Advertisement

Using WSAGetLastError();

Started by December 14, 2002 08:09 AM
5 comments, last by Leadorn 22 years, 1 month ago
Hi Im using WSAGetLastError to retrive error while using nonblocking sockets. Should I use wsagetlaserror after recv everytime? Is it slow? How is it done in unix without wsagetlasterror using recv and nonblocking sockets, you dont know when the client have lost there connection or not. If client disconnects i will know it becouse recv will return 0. Is there a return value in recv that says connections lost, with nonblocking. With blocking sockets there is SOCKET_ERROR, but recv always return SOCKET_ERROR with nonblocking.
The recv() function doesn't always return SOCKET_ERROR with nonblocking sockets.

You want to write something like this:

int nRet;nRet = recv(socket, buf, sizeof(buf), 0);if (nRet == SOCKET_ERROR){    if (WSAGetLastError() == WSAEWOULDBLOCK)    {         // no error, but recv would block    }    else    {         // uh oh, an error occured         printf("Socket error: %d\n", WSAGetLastError());    }}  


So if recv() is going to take too long, it returns SOCKET_ERROR and WSAGetLastError() returns WSAEWOULDBLOCK. If an error occurs, recv() returns SOCKET_ERROR and WSAGetLastError() returns the error code. If the client disconnects, recv() returns zero. If recv() succeeds, it returns the number of bytes of data that have been placed in your buffer.

In Unix, I think the global errno variable is set to EWOULDBLOCK and recv() returns -1.

[edited by - mattb on December 14, 2002 3:37:00 PM]
Advertisement
Oki, thanks


But is WSAGetlastError slow, does everyone else use this when using nonblocking sockets.


I could use select but then i have all my connections in a class with linked-list and so on. I cant use that with the select.
No it won''t slow your program down. It is a cheap call, and should be used liberally for error prevention and correction.

- Kevin "BaShildy" King
Game Programmer: DigiPen
www.mpogd.com
- Kevin "BaShildy" KingGame Programmer: DigiPenwww.mpogd.com
Oki thanks!

"Jag anar ugglor som får kläder"
If I request that recv bring me 12 byte will it only bring me those if there is 12 byte waiting in socket stream, or will it deliver whatever is in the socket stream?

If there are less byte waiting than 12 it will return socket_error. And nexttime it might be 12 byte waiting and there i got my 12 byte.

rec = recv(socket,databuffer,12,0);
Advertisement
The "len" parameter of the recv() function isn''t used to request a certain number of bytes, it is denotes the maximum number of bytes that can be received into your buffer without overflowing it.

So when you call recv() you aren''t specifying how many bytes to recv() you are specifying the maximum number of bytes your buffer can hold.

The recv() function will attempt to return as many bytes as have been received on the socket since the last successful call to recv(). On a successful call, the number of bytes returned will be anywhere between 1 and the value of the "len" parameter.

Regards.
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com

This topic is closed to new replies.

Advertisement