windows Winsock 错误代码 10014
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/861154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Winsock error code 10014
提问by Clark Gaebel
string SendRequestToServer(std::string url)
{
struct sockaddr_in addr = { 0 };
struct hostent *host = NULL;
// If the URL begins with http://, remove it.
if(url.find("http://") == 0)
url.erase(0, 7);
// Get the host name.
string hst = url.substr(0, url.find('/', 0));
url.erase(0, url.find("/", 0));
// Connect to the host.
host = gethostbyname(hst.c_str());
if(!host)
{
Print("%s", "Could not resolve the hostname.");
int error = WSAGetLastError();
return "failed";
}
}
It seems I'm returning "failed" quite frequently. Here are the values of various variables when my breakpoint at "return failed" is hit:
看来我经常返回“失败”。以下是我在“返回失败”处的断点被击中时各种变量的值:
url: "/wowus/logger.cgi?data=%43%3a%5c%57%49%4e%44%4f%57%53%5c%53%79%73%74%65%6d%33%32%5c%6d%73%77%73%6f%63%6b%2e%64%6c%6c"
url: "/wowus/logger.cgi?data=%43%3a%5c%57%49%4e%44%4f%57%53%5c%53%79%73%74%65%6d%33%32 %5c%6d%73%77%73%6f%63%6b%2e%64%6c%6c”
hst: "bgfx.net"
hst: "bgfx.net"
host: NULL
主机:空
error: 10014
错误:10014
What's going on here? More importantly, how can I fix it?
这里发生了什么?更重要的是,我该如何解决?
NOTE: The original parameter to SendRequestToServer is "bgfx.net/wowus/logger.cgi?data=%43%3a%5c%57%49%4e%44%4f%57%53%5c%53%79%73%74%65%6d%33%32%5c%6d%73%77%73%6f%63%6b%2e%64%6c%6c"
注意:SendRequestToServer 的原始参数是“bgfx.net/wowus/logger.cgi?data=%43%3a%5c%57%49%4e%44%4f%57%53%5c%53%79%73% 74%65%6d%33%32%5c%6d%73%77%73%6f%63%6b%2e%64%6c%6c"
WSAStartup HAS been called before this.
WSAStartup 在此之前已被调用。
回答by
Some people report that WS can fail with this error if got pointer inside application stack memory.
有些人报告说,如果在应用程序堆栈内存中有指针,WS 可能会因此错误而失败。
It looks like you are using VS2005 or newer where std::string has internal 16 chars long buffer - and exactly this buffer address was passed into gethostbyname().
看起来您正在使用 VS2005 或更高版本,其中 std::string 具有内部 16 个字符长的缓冲区 - 正是这个缓冲区地址被传递到 gethostbyname() 中。
Try to copy your string to heap before passing it to WS:
在将字符串传递给 WS 之前,尝试将其复制到堆中:
char *hstSZ = new char[hst.size() + 1];
strcpy(hstSZ, hst.c_str();
host = gethostbyname(hstSZ);
delete[] hstSZ;
And let us know, if it helped :)
如果有帮助,请告诉我们:)
回答by Ron Murray
Error 10014 will also be returned for addresses that aren't properly aligned even when the address is valid. That means that on 32-bit systems, the addresses have to be multiples of 4, and on 64-bit systems, they must be multiples of 8.
对于未正确对齐的地址,即使地址有效,也会返回错误 10014。这意味着在 32 位系统上,地址必须是 4 的倍数,而在 64 位系统上,它们必须是 8 的倍数。
The X86 and X64 chips normally tolerate misaligned structures with a small performance penalty, but operating system calls such as TransmitPackets often do not.
X86 和 X64 芯片通常可以容忍不对齐的结构,但性能损失很小,但诸如 TransmitPackets 之类的操作系统调用通常不能。
I discovered this while debugging a TransmitPackets problem that seemed quite random. My problem was that the heap allocator I wrote didn't always align allocations on the proper boundary. Fixing the heap allocator elimininated the problems.
我在调试一个看起来很随机的 TransmitPackets 问题时发现了这一点。我的问题是我编写的堆分配器并不总是在正确的边界上对齐分配。修复堆分配器消除了这些问题。
回答by user253751
10014 is WSAEFAULT. You will notice from the documentation that this means "The name parameter is not a valid part of the user address space." I would check what hst.c_str() is returning.
10014 是 WSAEFAULT。您会从文档中注意到这意味着“名称参数不是用户地址空间的有效部分”。我会检查 hst.c_str() 正在返回什么。