- 论坛徽章:
- 0
|
代码的主要功能是利用gethostname()函数得到主机名,然后根据得到的主机名利用gethostbyname()函数得到主机的IP,代码如下,单步调试出现的错误见附件,在执行for语句的时候出现unhandled exception in ip.exe:0xC0000005: Access Violation 错误,请教高手是怎么回事呢?
#ifndef _WINSOCK_H
#define _WINSOCK_H
#include
#pragma comment (lib,"Ws2_32.lib")
#endif
#include
int main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested=MAKEWORD(2,2);
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{
fprintf(stdout,"cannot link the library!\n");
return 0;
}
char zjm[1024];
if(gethostname(zjm,sizeof(zjm))==SOCKET_ERROR )
{
fprintf(stdout,"gethostname() failed %d!\n",WSAGetLastError());
return 0;
}
fprintf(stdout,"hostname is %s!\n",zjm);
struct hostent * phost=NULL;
int i;
if(gethostbyname(zjm)==NULL)
{
fprintf(stdout,"gethostbyname() failed %d!\n",WSAGetLastError());
return 0;
}
for(i=0;phost->h_addr_list[i]!=NULL;i++)
{
char* psz=NULL;
psz=inet_ntoa(*(struct in_addr *)phost->h_addr_list[i]);
fprintf(stdout,"hostip is %s!\n",psz);
}
return 1;
} |
-
未命名.JPG
(67.27 KB, 下载次数: 21)
debug提示错误图
|