I am writing C++ socket code using Visual Studio Express 2013 in a .dll project. I am at the point where I am getting an error at this sendto
function:
/* Send data back */
if (sendto(sd, (const char *)sendBuffer,(int)sizeof(sendBuffer), 0,
(struct sockaddr *)&client, sizeof(client)) ==
SOCKET_ERROR)
{
err = WSAGetLastError();
closesocket(sd);
WSACleanup();
throw std::runtime_error("Error sending datagram.");
}
I know that we can get an error code with WSAGetLastError()
, but if I initialize it to a variable I just seem to get junk numbers. How do I extract the sendto
error? I have unit tests written to test this dll, but I'm not sure if there is a way I can just print out the error.
Edit: So after changing the condition to == not != it no longer fails at sendto
but now fails at
bytes_received = recvfrom(sd, readBuffer, NTP_PACKET_MAX, 0, (struct sockaddr *)&client, &len);
if (bytes_received == SOCKET_ERROR)
{
err = WSAGetLastError();
closesocket(sd);
WSACleanup();
throw std::runtime_error("Could not receive datagram.");
}
Like in the original question, i'm unsure of how to get the proper error. When debugging the recvfrom
function, I hover over err
and it says the value it holds is 114351196. From what I can see, that is not a valid Windows Socket Error Code.