关于gethostname函数失败的问题
2014-05-07 10:36
429 查看
调用gethostname之前, 要先调用WSAStartup才可以, 否则gethostname会失败!
下面是正确的代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Winsock2.h>
#include <windows.h>
#pragma comment(lib, "Ws2_32")
int main()
{
WSADATA wsData;
::WSAStartup(MAKEWORD(2,2), &wsData);
char szIP[32] = {0};
char szHostName[32] = {0};
int iResult = ::gethostname(szHostName, sizeof(szHostName));
if (iResult != 0)
{
printf("error/n");
return -1;
}
printf("%s/n", szHostName);
hostent *pHost = ::gethostbyname(szHostName);
::WSACleanup();
return 0;
}
下面是正确的代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Winsock2.h>
#include <windows.h>
#pragma comment(lib, "Ws2_32")
int main()
{
WSADATA wsData;
::WSAStartup(MAKEWORD(2,2), &wsData);
char szIP[32] = {0};
char szHostName[32] = {0};
int iResult = ::gethostname(szHostName, sizeof(szHostName));
if (iResult != 0)
{
printf("error/n");
return -1;
}
printf("%s/n", szHostName);
hostent *pHost = ::gethostbyname(szHostName);
::WSACleanup();
return 0;
}
相关文章推荐
- 关于gethostname函数失败的问题
- 关于gethostname函数失败的问题
- gethostname()函数出错,但返回success的问题解决
- 关于MySQL问题:can't get hostname for your address
- 使用GetOpenFileName和GetSaveFileName函数会改变当前目录的问题
- MySQL有关1042 Can’t get hostname for your address的问题分析解决过程
- server端和clien端关于getsockname和getpeername函数的用法
- 关于调用gethostbyname失败的原因:Host name lookup failure.
- MySQL有关1042 Can’t get hostname for your address的问题分析解决过程
- 关于ffmpeg avcodec_open2函数失败的问题
- 关于在用curl函数post网页数据时,遇上表单提交 type为submit 类型而且没有name和id时可能遇到的问题及其解决方法
- ie的大问题,关于document。getElementById和getElementsByName
- 关于Ubuntu apt-get update 失败,问题解决
- gethostname()函数出错:WSAGetLastError 10093 解决方法
- 使用gethostname()函数和gethostbyname()函数获取主机相关信息
- 关于调用js函数失败问题及解决
- 关于getElementByClassName的兼容性问题
- 日常工作问题总结(三十一)windows获取路径的几个函数GetCurrentDirectory,GetModuleFileName,GetFullPathName
- 关于IE中getElementsByClassName不能用的问题
- MFC 关于GetAsyncKeyState函数的问题