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

  • 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 name

SYNOPSIS
       #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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值