Click to See Complete Forum and Search --> : WSAGetLastError() and WSACleanup()


sleak
March 29th, 2007, 01:28 AM
I am just wondering if anyone here would please be able to tell me what will happen when I call WSACleanup() as a result of a socket related failure (some WinSock function returning an error). will a call to WSACleanup() clean up any last errors? for example, right now here is my code for one of the functions:


if(bind(lsock, (PSOCKADDR)&sin, sizeof(SOCKADDR_IN)) == SOCKET_ERROR) {
err = WSAGetLastError();

closesocket(lsock);
WSACleanup();

return err;
}


looks ok, but I'm crazy for finding and using the smallest, most efficient methods of getting stuff done. err is a local variable declared as an int. the reason why I'm asking this question is because I'd like to know if I could omit the local variable and do something like this:


if(bind(lsock, (PSOCKADDR)&sin, sizeof(SOCKADDR_IN)) == SOCKET_ERROR) {
closesocket(lsock);
WSACleanup();

return WSAGetLastError();
}


its a pretty simple question, however I don't see anything mentioned on msdn about WSACleanup() and last errors. do you think it would be okay if I used the second code, and that WSAGetLastError() will still return the error code for the error that caused bind() to fail (the last error)?

another question that comes to mind is will I even be able to call WSAGetLastError() after a call to WSACleanup() without some access violation or the like?


any help here is greatly appreciated. thank you in advance.

humptydumpty
March 29th, 2007, 01:48 AM
First thing WSACleanup() is not used for cleaning your Error.it free any resources allocated on behalf of the application and to deregister your App from a Windows Sockets implementation in case when you are working with Sockets.

WSAGetLastError() this function gets the error status for the last operation that failed.if some app return Error in that case you can use GetLastError() and can check the Condition of your Error.

Thanx

sleak
March 29th, 2007, 02:05 AM
I don't think you understood my question. I don't want WSACleanup() to clean the last error, I am simply asking if it will do just that.

both codes do the same thing, they handle the SOCKET_ERROR return value by cleaning up and returning the last error code. the first one uses a local variable that is only used for this purpose in the function that it is local to, which is to ensure that the last socket related error is retained. the second doesn't use a local variable, and because the WSAGetLastError() is placed after the WSACleanup(), I don't know if WSAGetLastError() will return anything useful here (WSACleanup() might have already got to the last error, I don't know if it frees last errors or what).

for the more visual learners, here is the second code commented:


if(bind(lsock, (PSOCKADDR)&sin, sizeof(SOCKADDR_IN)) == SOCKET_ERROR) {
closesocket(lsock);
WSACleanup(); /* will this free the last error code? */

return WSAGetLastError(); /* if WSACleanup() freed the last error code, this function is 100% pointless and will likely return undesired results. Or it might not even function at all, it could trigger some runtime error like an access violation due to the WinSock DLL no longer being available (as a result of the WSACleanup()). */
}

humptydumpty
March 29th, 2007, 02:19 AM
That is what i said in my previous Post .better once again you read the Post so you can understand the Difference Between these two.

Difference between Freeing your Allocated resource and to get error .if WSACleanup() failed then you use WSAGetLastError() to retrive the last error mean why WSACleanup() failed.in general case WSACleanup() comes before WSAStartup().means at the time when you start doing your Work Before that you want to free all alocated resource so you can move in a efficient manner

Thanx