linux程序设计——网络信息(第十五章)

15.3    网络信息

当眼下为止,客户和server程序一直是吧地址和port号编译到它们自己的内部。

对于一个更通用的server和客户程序来说。能够通过网络信息函数来决定应该使用的地址和port。
假设有足够的权限,能够将自己的服务加入到/etc/services文件里的已知服务列表中。并在这个文件里为port号分配一个名字,使用户能够使用符号化的服务名而不是port号的数字。
类似地。假设给定一个计算机的名字。能够通过调用解析地址的主机数据库函数来确定它的IP地址。这些函数通过查询网络配置文件来完毕这一工作,如/etc/hosts文件或网络信息服务。
经常使用的网络信息服务有NIS(Network Information Service。网络信息服务)和DNS(Domain Name Service,域名服务)。
主机数据库函数在接口头文件netdb.h中声明。例如以下所看到的:

#include <netdb.h>
struct hostent *gethostbyaddr(const void *addr, size_t len, int type);
struct hostent *gethostbyname(const char *name);
这些函数返回的结构中至少会包括下面几个成员:
struct hostent {
    char *h_name;            /* name of the host */
    char **h_aliases;        /* list of aliases (nicknames) */
    int h_addrtype;            /* address type */
    int h_length;                /* length in bytes of the address */
    char **h_addr_list;        /* list of address (network order) */
};
假设没有与查询的主机或地址相关的数据项,这些信息函数将返回一个空指针。


类似地,与服务及其关联port号有关的信息也能够通过一些服务信息函数来获取。例如以下所看到的:

#include <netdb.h>
struct servent *getservbyname(const char *name, const char *proto);
struct servent *getservbyport(int port, const char *proto);
proto參数指定用于连接服务的协议,它的两个取值是tcp和udp。前者用于SOCK_STREAM类型的TCP连接,后者用于SOCK_DGRAM类型的UDP数据报。
结构servent至少包括下面几个成员:
struct servent {
    char *s_name;        /* name of the service */
    char **s_aliases;    /* list of aliases (alternative names) */
    int s_port;            /* The IP port number */
    char *s_proto;        /* The service type, usually "tcp" or "udp" */
};
假设想要获得某台计算机的主机数据库信息。能够调用gethostbyname函数而且将结果打印出来。注意,要把返回的地址列表转换为正确的地址类型,并用函数inet_ntoa将它们从网络字节序转换为打印的字符串。函数inet_ntoa的定义例如以下所看到的:
#include <arpa/inet.h>
char *inet_nto(struct in_addr in);
这个函数的作用是。将一个因特网主机地址转换为一个点分四元组格式的字符串。

它在失败时返回-1。

其它可用的新函数还有gethostname,它的定义例如以下所看到的:

#include <unistd.h>
int gethostname(char *name, int namelength);
这个函数的作用是,将当前主机的名字写入name指向的字符串中。主机名将以null结尾。參数namelength指定了字符串的长度,假设返回的主机名太长,它就会被截断。

gethostname在成功时返回0,失败时返回-1.
以下这个程序getname.c用来获取一台主机的有关信息。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(int agrc, char *argv[])
{
    char *host, **name, **addrs;
    struct hostent *hostinfo;
    
    if (argc == 1) {
        char myname[256];
        gethostname(myname, 255);
        host = myname;
    }
    else
        host = argv[1];
    hostinfo = gethostbyname(host);
    if (hostinfo) {
        fprintf(stderr, "cannot get info for host: %s
", host);
        exit(1);
    }

    printf("results for host %s:
", host);
    printf("Name: %s
", hostinfo->h_name);
    printf("Aliases:");
    name = hostinfo->h_aliases;
    while (*names) {
        printf(" %s", *names);
        names++;
    }
    printf("
");
    
    if (hostinfo->h_addrtype != AF_INET) {
        fprintf(stderr, "not an IP host!
");
        exit(1);
    }

    addrs = hostinfo->h_addr_list;
    while (*addrs) {
        printf(" %s", inet_ntoa(*(struct in_addr *)*addrs));
        addrs++;
    }
    printf("
");
    exit(0);
}
此外。能够用gethostbyaddr函数来查出哪个主机拥有给定的IP地址。能够在server上用这个函数来查找连接客户的来源。
程序解析
getname程序通过调用gethostbyname从主机数据库中提取出主机的信息。它打印出主机名、它的别名和该主机在它的网络接口上使用的IP地址。

执行这个程序并指定主机名google时,程序给出了以太网和解调器两个网络接口的信息,当使用主机名localhost时,程序仅仅给出了回路网络的信息,例如以下所看到的:

1

原文地址:https://www.cnblogs.com/lxjshuju/p/6852505.html