从 recvfrom 获取未记录的错误代码
Getting undocumented error code from recvfrom
当尝试使用 recvfrom
函数读取 UDP 数据包时 returns -1 指示错误。我当然会打电话给 WSAGetLastError
来找出问题所在。报告的错误编号是 183。我似乎找不到关于该编号含义的任何参考。
编辑:
while (bytesRecv != SOCKET_ERROR)
{
// get data from the server
bytesRecv = recvfrom(m_socket, (char*)&receiveData, sizeof(ReceiveData), 0, (struct sockaddr *) &server_addr, &server_addr_len);
logError("Bytes recieved: ", bytesRecv);
// if data was recieved from the server
if (bytesRecv > 0)
{
//Data packet processing code
}
else
{
if (bytesRecv == SOCKET_ERROR)
{
logError("Error: Reading data: ", WSAGetLastError());
}
}
}
编辑:
void logError(const std::string &text, int errorCode)
{
std::ofstream log_file("error_log_file.txt", std::ios_base::out | std::ios_base::app);
log_file << text << errorCode << "\n";
}
问题不在于 WSAGetLastError()
本身。真正的问题是您在调用 WSAGetLastError()
之前调用了 logError()
,并且 logError()
最终将最后一个错误代码重置为 183。
logError()
使用 std::ofstream
打开文件进行追加。在 Windows 上,该操作最终将使用 OPEN_ALWAYS
标志调用 CreateFile()
,其文档指出:
Opens a file, always.
If the specified file exists, the function succeeds and the last-error code is set to ERROR_ALREADY_EXISTS (183).
If the specified file does not exist and is a valid path to a writable location, the function creates a file and the last-error code is set to zero.
...
If the function fails, the return value is INVALID_HANDLE_VALUE. To get extended error information, call GetLastError.
在内部,WSAGetLastError()
简单地映射到 GetLastError()
(一个 well-known 但未记录的实现细节)。所以,无论CreateFile()
打开文件成功与否,WSAGetLastError()
报告的错误代码都会重置为打开操作的结果。
你对 logError()
的调用是在错误的地方。它需要在你的 if (bytesRecv > 0)
块内移动(顺便说一句,UDP 支持 0 长度数据报,所以你应该使用 >=
而不是 >
):
while (true)
{
// get data from the server
bytesRecv = recvfrom(m_socket, (char*)&receiveData, sizeof(ReceiveData), 0, (struct sockaddr *) &server_addr, &server_addr_len);
// if data was received from the server
if (bytesRecv >= 0)
{
logError("Bytes received: ", bytesRecv); // <-- moved here!!!
//Data packet processing code
}
else // if (bytesRecv == SOCKET_ERROR)
{
logError("Error: Reading data: ", WSAGetLastError());
break;
}
}
或者:
while (true)
{
// get data from the server
bytesRecv = recvfrom(m_socket, (char*)&receiveData, sizeof(ReceiveData), 0, (struct sockaddr *) &server_addr, &server_addr_len);
// if data was received from the server
if (bytesRecv == SOCKET_ERROR)
{
logError("Error: Reading data: ", WSAGetLastError());
break;
}
logError("Bytes received: ", bytesRecv); // <-- moved here!!!
//Data packet processing code
}
当尝试使用 recvfrom
函数读取 UDP 数据包时 returns -1 指示错误。我当然会打电话给 WSAGetLastError
来找出问题所在。报告的错误编号是 183。我似乎找不到关于该编号含义的任何参考。
编辑:
while (bytesRecv != SOCKET_ERROR)
{
// get data from the server
bytesRecv = recvfrom(m_socket, (char*)&receiveData, sizeof(ReceiveData), 0, (struct sockaddr *) &server_addr, &server_addr_len);
logError("Bytes recieved: ", bytesRecv);
// if data was recieved from the server
if (bytesRecv > 0)
{
//Data packet processing code
}
else
{
if (bytesRecv == SOCKET_ERROR)
{
logError("Error: Reading data: ", WSAGetLastError());
}
}
}
编辑:
void logError(const std::string &text, int errorCode)
{
std::ofstream log_file("error_log_file.txt", std::ios_base::out | std::ios_base::app);
log_file << text << errorCode << "\n";
}
问题不在于 WSAGetLastError()
本身。真正的问题是您在调用 WSAGetLastError()
之前调用了 logError()
,并且 logError()
最终将最后一个错误代码重置为 183。
logError()
使用 std::ofstream
打开文件进行追加。在 Windows 上,该操作最终将使用 OPEN_ALWAYS
标志调用 CreateFile()
,其文档指出:
Opens a file, always.
If the specified file exists, the function succeeds and the last-error code is set to ERROR_ALREADY_EXISTS (183).
If the specified file does not exist and is a valid path to a writable location, the function creates a file and the last-error code is set to zero.
...
If the function fails, the return value is INVALID_HANDLE_VALUE. To get extended error information, call GetLastError.
在内部,WSAGetLastError()
简单地映射到 GetLastError()
(一个 well-known 但未记录的实现细节)。所以,无论CreateFile()
打开文件成功与否,WSAGetLastError()
报告的错误代码都会重置为打开操作的结果。
你对 logError()
的调用是在错误的地方。它需要在你的 if (bytesRecv > 0)
块内移动(顺便说一句,UDP 支持 0 长度数据报,所以你应该使用 >=
而不是 >
):
while (true)
{
// get data from the server
bytesRecv = recvfrom(m_socket, (char*)&receiveData, sizeof(ReceiveData), 0, (struct sockaddr *) &server_addr, &server_addr_len);
// if data was received from the server
if (bytesRecv >= 0)
{
logError("Bytes received: ", bytesRecv); // <-- moved here!!!
//Data packet processing code
}
else // if (bytesRecv == SOCKET_ERROR)
{
logError("Error: Reading data: ", WSAGetLastError());
break;
}
}
或者:
while (true)
{
// get data from the server
bytesRecv = recvfrom(m_socket, (char*)&receiveData, sizeof(ReceiveData), 0, (struct sockaddr *) &server_addr, &server_addr_len);
// if data was received from the server
if (bytesRecv == SOCKET_ERROR)
{
logError("Error: Reading data: ", WSAGetLastError());
break;
}
logError("Bytes received: ", bytesRecv); // <-- moved here!!!
//Data packet processing code
}