我需要获取我正在使用gethostname函数
的系统主机名但它失败了,错误代码 10093 ,这是
WSANOTINITIALISED 10093
尚未执行成功的WSAStartup。应用程序都有 没有调用WSAStartup或WSAStartup失败。申请可能是 访问当前活动任务不拥有的套接字(即, 尝试在任务之间共享套接字),或者调用了WSACleanup 太多次了。
以下是我的程序代码:
#include <Winsock2.h>
#include <ws2tcpip.h>
#include <Windows.h>
#pragma comment(lib, "Ws2_32.lib")
int main()
{
char hostname[1024];
hostname[1023] = '\0';
gethostname(hostname, 1023);
int err = WSAGetLastError();
}
可能导致此失败的原因是什么?
修改
在gethostname函数调用之前添加以下代码解决了这个问题。
if (WSAStartup (MAKEWORD(2,2), &WSAData) != 0)
{
MessageBox (NULL, TEXT("WSAStartup failed!"), TEXT("Error"), MB_OK);
return FALSE;
}
答案 0 :(得分:2)
它写在您发布的链接中:
在使用此功能之前,必须成功进行
WSAStartup
调用。
致电WSAStartup
,检查其返回代码,如果一切顺利,请致电gethostname
。
答案 1 :(得分:1)
错误消息说明了一切。您需要在WSAStartup
:http://msdn.microsoft.com/en-gb/library/windows/desktop/ms742213(v=vs.85).aspx
gethostname
答案 2 :(得分:0)
阅读错误消息:应用程序未调用WSAStartup