I might be mistaken, but I believe WSAGetLastError() is not guaranteed to return 0 if there is no error. It stores the previous error that has occurred in the thread indefinitely, and may or may not return the value 0 if no error has occured at all.
That said, the following would be an invalid assumption
1 2 3
|
if(WSAGetLastError() != 0) { //I assume this is to see if there has been an error
//...
}
|
A better way to check for errors is to use the return codes for individual winsock functions, such as what you did with line 17, checking to see if WSAStartup had an error. WSAStartup() guarantees that it will return a nonzero value if there has been an error. In which case, we call WSAGetLastError() and return.
For example, gethostbyname() returns a NULL pointer if there was an error. So check to see if an error has occurred on that function call:
1 2 3 4
|
if((remoteHost = gethostbyname(host_name) == NULL) {
cout << "gethosbyname failed: " << WSAGetLastERror() << endl;
return 1;
}
|
Doing this will narrow down what is throwing an error (if an actual error is being thrown at all).
EDIT:
Also, of course your code would show that there has been an error #0.
If, in fact, WSAGetLastError() returns zero, your if statement checks whether it does not equal zero. That means the else section of the code will be executed
because WSAGetLastError equals 0. And then the else body prints out that 0.
Point being, there is probably no error occurring.