网络编程中本机地址常用函数

来源:互联网 发布:js中array的map方法 编辑:程序博客网 时间:2024/06/05 10:51
  • gethostbyname根据给定的主机名,获取主机信息。已经过时,仅用于IPv4,且线程不安全。
#include <stdio.h>#include <netdb.h>#include <arpa/inet.h>extern int h_errno;int main(int argc, char *argv[]){    struct hostent *host;    char str[128];    host = gethostbyname(argv[1]);    printf("%s\n", host->h_name);    while (*(host->h_aliases) != NULL)        printf("%s\n", *host->h_aliases++);    switch (host->h_addrtype) {        case AF_INET:            while (*(host->h_addr_list) != NULL)            printf("%s\n", inet_ntop(AF_INET, (*host->h_addr_list++), str, sizeof(str)));        break;        default:            printf("unknown address type\n");            break;    }    return 0;}
   gethostname、gethostbyname、gethostbyaddr       The  domain  name  queries  carried out by gethostbyname() and gethostbyaddr() use a combination of any or all of the name server named(8), a broken out       line from /etc/hosts, and the Network Information Service (NIS or YP), depending upon the contents of the order line  in  /etc/host.conf.   The  default       action is to query named(8), followed by /etc/hosts.       The hostent structure is defined in <netdb.h> as follows:              struct hostent {                      char    *h_name;        /* official name of host */                      char    **h_aliases;    /* alias list */                      int     h_addrtype;     /* host address type */                      int     h_length;       /* length of address */                      char    **h_addr_list;  /* list of addresses */              }              #define h_addr  h_addr_list[0]  /* for backward compatibility */       The members of the hostent structure are:
  • gethostbyaddr函数。
    此函数只能获取域名解析服务器的url和/etc/hosts里登记的IP对应的域名。
#include <stdio.h>#include <netdb.h>#include <arpa/inet.h>extern int h_errno;int main(int argc, char *argv[]){    struct hostent *host;    char str[128];    struct in_addr addr;    inet_pton(AF_INET, argv[1], &addr);    host = gethostbyaddr((char *)&addr, 4, AF_INET);    printf("%s\n", host->h_name);    while (*(host->h_aliases) != NULL)        printf("%s\n", *host->h_aliases++);    switch (host->h_addrtype) {        case AF_INET:            while (*(host->h_addr_list) != NULL)            printf("%s\n", inet_ntop(AF_INET, (*host->h_addr_list++), str, sizeof(str)));            break;        default:            printf("unknown address type\n");            break;    }    return 0;}
  • getservbyname
  • getservbyport
    根据服务程序名字或端口号获取信息。使用频率不高。
  • getaddrinfo
  • getnameinfo
  • freeaddrinfo
    可同时处理IPv4和IPv6,线程安全的。

  • 套接口和地址关联

    • getsockname
      根据accpet返回的sockfd,得到临时端口号
getsockname & getpeername 获取本地的地址(注意是已连接以后的套接字)NAME       getsockname - get socket nameSYNOPSIS       #include <sys/socket.h>       int getsockname(int s, struct sockaddr *name, socklen_t *namelen);    struct sockaddr_in localaddr;    socklen_t addrlen = sizeof(localaddr);    //获取本地的地址    if (getsockname(sock, (struct sockaddr*)&localaddr, &addrlen) < 0)        ERR_EXIT("getsockname");    printf("ip=%s port=%d\n", inet_ntoa(localaddr.sin_addr), ntohs(localaddr.sin_port));
  • getpeername
    根据accpet返回的sockfd,得到远端链接的端口号,在exec后可以获取客户端信息。

  • 总体示例代码

int getlocalip(char *ip){    char host[100] = {0};    if (gethostname(host, sizeof(host)) < 0)        return -1;    struct hostent *hp;    if ((hp = gethostbyname(host)) == NULL)        return -1;    strcpy(ip, inet_ntoa(*(struct in_addr*)hp->h_addr));    return 0;}int main(void){    char host[100] = {0};    if (gethostname(host, sizeof(host)) < 0)        ERR_EXIT("gethostname");    printf("\n\n\nhost:%s \n", host);    struct hostent *hp;    if ((hp = gethostbyname(host)) == NULL)        ERR_EXIT("gethostbyname");    int i = 0;    while (hp->h_addr_list[i] != NULL)    {         //char *inet_ntoa(struct in_addr in); 要求填入一个结构体元素        printf("%s\n", inet_ntoa(*(struct in_addr*)hp->h_addr_list[i]));        i++;    }    char ip[16] = {0};    getlocalip(ip);    printf("localip=%s\n", ip);    return 0;}
0 0