#include <winsock2.h>
#include <stdio.h>
#include <stdlib.h>
void main()
{
WSADATA wsadata;
int n;
char hostname[256];
hostent *phostent;
protoent *pprotoent;
struct sockaddr_in sa;
if(WSAStartup(MAKEWORD(2,2),&wsadata)!=0)
{
printf("wsastartup() failed!!/n");
return;
}
printf("--------------------------/n");
if(gethostname(hostname,sizeof(hostname))!=0)
{
printf("gethostbyname() error!!/n");
return;
}
printf("local host name:%s/n",hostname);
printf("------------------/n");
phostent=gethostbyname(hostname);
if(phostent==NULL)
{
printf("gethostbyname() error!!/n");
return;
}
printf("name:%s/nalianses:%s/naddrtype:%d/nlength:%d/n",phostent->h_name,phostent->h_aliases,phostent->h_addrtype,phostent->h_length);
for(n=0;phostent->h_addr_list[n];n++)
{
memcpy(&sa.sin_addr.s_addr,phostent->h_addr_list[n],phostent->h_length);
printf("address:%s/n",inet_ntoa(sa.sin_addr));
}
printf("--------------------/n");
pprotoent=getprotobyname("tcp");
if(pprotoent==NULL)
{
printf("getprotobyname() failed!!/n");
return;
}
printf("name:%/nproto:%d/n",pprotoent->p_name,pprotoent->p_proto);
for(n=0;pprotoent->p_aliases[n];n++)
{
printf("aliases:%s/n",pprotoent->p_aliases[n]);
}
WSACleanup();
}
gethostname() gethostbyname() getprotobyname()用法
最新推荐文章于 2022-03-24 10:37:22 发布