gethostname() gethostbyname() getprotobyname()用法!(VC++)

#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();

}

你可能感兴趣的:(职场,网络编程,vc++,休闲)