Unix Network Programming Episode 76

这篇具有很好参考价值的文章主要介绍了Unix Network Programming Episode 76。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

We encourage the use of getaddrinfo (Section 11.6(See 8.9.6)) in new programs.

#include <netdb.h>
struct hostent *gethostbyname (const char *hostname);

The non-null pointer returned by this function points to the following hostent structure:

struct hostent {
	char *h_name; /* official (canonical) name of host */
	char **h_aliases; /* pointer to array of pointers to alias names */
	int h_addrtype; /* host address type: AF_INET */
	int h_length; /* length of address: 4 */
	char **h_addr_list; /* ptr to array of ptrs with IPv4 addrs */
};

gethostbyname differs from the other socket functions that we have described in that it does not set errno when an error occurs. Instead, it sets the global integer h_errno to one of the following constants defined by including <netdb.h>:

  • HOST_NOT_FOUND
  • TRY_AGAIN
  • NO_RECOVERY
  • NO_DATA (identical to NO_ADDRESS)

The NO_DATA error means the specified name is valid, but it does not have an A record. An example of this is a hostname with only an MX record.
Most modern resolvers provide the function hstrerror, which takes an h_errno value as its only argument and returns a const char * pointer to a description of the error.

#include "unp.h"

int main(int argc, char **argv)
{
    char *ptr, **pptr;
    char str[INET_ADDRSTRLEN];
    struct hostent *hptr;

    while(--argc>0)
    {
        ptr=*++argv;
        if((hptr=gethostbyname(ptr)==NULL)
        {
            err_msg("gethostbyname error for host: %s: %s",ptr, hstrerror,(h_errno));
        }
        continue;
    }
    printf("official hostname: %s\n", hptr->h_name);

    for(pptr=hptr->h_aliases;*pptr!=NULL;pptr++)
        printf("\talias:%s\n", *pptr);
    
    switch(hptr->h_addrtype)
    {
        case AF_INET:
            pptr=hptr->h_addr_list;
            for(;*pptr!=NULL;pptr)
            {
                printf("\taddress: %s\n",Inet_ntop(hptr->h_addrtype,*pptr, str, sizeof(str)));
            }
            break;
        default:
            err_ret("unknown address type");
            break;
    }
    return 0;
}

Call gethostbyname and print returned information文章来源地址https://www.toymoban.com/news/detail-703299.html

到了这里,关于Unix Network Programming Episode 76的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Unix Network Programming Episode 88

    ‘inetd’ Daemon On a typical Unix system, there could be many servers in existence, just waiting for a client request to arrive. Examples are FTP, Telnet, Rlogin, TFTP, and so on. With systems before 4.3BSD, each of these services had a process associated with it. This process was started at boot-time from the file /etc/rc, and each process did nearly ide

    2024年03月10日
    浏览(70)
  • unix网络编程-简易服务器与客户端程序解析

    a -- address f -- file        eg: fputs() -- file put stream fd -- file descriptor h - host(主机) in/inet -- internet        eg: sockaddr_in; inet_aton n -- network(网络字节序)/numeric(数值) p -- protocol(协议)/presentation(表达/呈现形式) s -- socket        eg: sin -- socket internet t -- type,用于指定某种

    2024年01月16日
    浏览(54)
  • UNIX网络编程卷一 学习笔记 第三十章 客户/服务器程序设计范式

    开发一个Unix服务器程序时,我们本书做过的进程控制: 1.迭代服务器(iterative server),它的适用情形极为有限,因为这样的服务器在完成对当前客户的服务前无法处理已等待服务的新客户。 2.并发服务器(concurrent server),为每个客户调用fork派生一个子进程。传统上大多U

    2024年02月09日
    浏览(38)
  • 一文搞清UNIX/Linux与Windows文件换行符格式差异

    当一个文件在Windows和Linux上交替操作后,经常遇到一些莫名其妙的问题,如shell脚本无法执行,找不到shell脚本等问题,本文j谨就这一问题做一总结,供各位参考; 本博客地址,https://blog.csdn.net/qxhgd,欢迎各位关注,转发请注明出处。 换行符是行尾 (EOL),是一个特殊的字

    2024年02月15日
    浏览(30)
  • 【Socket】Unix环境下搭建简易本地时间获取服务

    本文搭建一个Unix环境下的、局域网内的、简易的本地时间获取服务。 主要用于验证: 当TCP连接成功后,可以在两个线程中分别进行读操作、写操作动作 当客户端自行终止连接后,服务端会在写操作时收到 SIGPIPE 信号 当客户端执行shutdown写操作后,客户端会在写操作时收到

    2024年02月04日
    浏览(28)
  • UNIX家族?Windows NT家族?一文讲清操作系统繁杂的家族史

    本专栏更新速度慢,简单讲讲操作系统的那些事,让不是做操作系统开发的同学也能大概认识操作系统这个出现在生活各处的东西 浅淡操作系统系列第0篇 目录 关于专栏 贝尔实验室 UNIX Linux BSD Windows NT 结语 快捷翻页 参考文章 讲操作系统肯定离不开贝尔实验室了,贝尔实验

    2024年02月13日
    浏览(42)
  • Tenable Nessus 10.5.3 (Unix, Linux, Windows) - #1 漏洞评估解决方案

    Tenable Nessus 10.5.3 (Unix, Linux, Windows) - #1 漏洞评估解决方案 发布 Nessus 试用版自动化安装程序,支持 macOS Ventura、RHEL 9 和 Ubuntu 22.04 请访问原文链接:https://sysin.org/blog/nessus-10/,查看最新版。原创作品,转载请保留出处。 作者主页:sysin.org Nessus 漏洞评估领域的全球黄金标准 针

    2024年02月15日
    浏览(23)
  • OpenText Exceed TurboX(ETX)—— 适用于 UNIX、Linux 和 Windows 的远程桌面解决方案

    由于新技术的采用,以及商业全球化和全球协作的现实,几乎所有企业(无论其规模和所处行业)的员工的工作方式、时间和地点都发生了重大变化。业务领导者正在推动其 IT 部门提出解决方案,以帮助其远程员工提高工作效率,同时确保公司系统安全可用并提供卓越的用户

    2024年02月09日
    浏览(25)
  • 【MySQL】MySQL在Linux/UNIX和 Windows上的安装,验证安装和登录 MySQL的详细讲解

    作者简介: 辭七七,目前大一,正在学习C/C++,Java,Python等 作者主页: 七七的个人主页 文章收录专栏: 七七的闲谈 欢迎大家点赞 👍 收藏 ⭐ 加关注哦!💖💖 所有平台的 MySQL 下载地址为:MYSQL下载网址挑选你需要的 MySQL Community Server 版本及对应的平台。 注意: 安装过程

    2024年02月11日
    浏览(29)
  • 了解Linux及Unix

    目前流行的服务器和 PC 端操作系统有 Linux、Windows、UNIX 等,手机操作系统有 Android、iOS、Windows Phone(简称 WP),嵌入式操作系统有 Windows CE、PalmOS、eCos、uClinux 等。 之前浅浅地接触了docker和k8s,运行环境为linux操作系统,就想着了解一下linux及unix,纯粹出于好奇哦,所以我准

    2024年02月12日
    浏览(24)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包