Unix Network Programming Episode 79

2023-10-09 03:15

本文主要是介绍Unix Network Programming Episode 79,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

‘gai_strerror’ Function

The nonzero error return values from getaddrinfo have the names and meanings shown in Figure 11.7. The function gai_strerror takes one of these values as an argument and returns a pointer to the corresponding error string.

#include <netdb.h>
const char *gai_strerror (int error);
‘freeaddrinfo’ Function

All the storage returned by getaddrinfo, the addrinfo structures, the ai_addr structures, and the ai_canonname string are obtained dynamically (e.g., from malloc). This storage is returned by calling freeaddrinfo.

#include <netdb.h>
void freeaddrinfo (struct addrinfo *ai);

ai should point to the first addrinfo structure returned by getaddrinfo. All the structures in the linked list are freed, along with any dynamic storage pointed to by those structures (e.g., socket address structures and canonical hostnames).

‘getaddrinfo’ Function: IPv6

The POSIX specification defines the getaddrinfo function and the information it returns for both IPv4 and IPv6.

  • getaddrinfo is dealing with two different inputs: the type of socket address structure the caller wants back and the type of records that should be searched for in the DNS or other database.
  • The address family in the hints structure provided by the caller specifies the type of socket address structure that the caller expects to be returned. If the caller specifies AF_INET, the function must not return any sockaddr_in6 structures; if the caller specifies AF_INET6, the function must not return any sockaddr_in structures.
  • POSIX says that specifying AF_UNSPEC will return addresses that can be used with any protocol family that can be used with the hostname and service name. This implies that if a host has both AAAA records and A records, the AAAA records are returned as sockaddr_in6 structures and the A records are returned as sockaddr_in structures. It makes no sense to also return the A records as IPv4-mapped IPv6 addresses in sockaddr_in6 structures because no additional information is being returned: These addresses are already being returned in sockaddr_in structures.
  • This statement in the POSIX specification also implies that if the AI_PASSIVE flag is specified without a hostname, then the IPv6 wildcard address (IN6ADDR_ANY_INIT or 0::0) should be returned as a sockaddr_in6 structure, along with the IPv4 wildcard address (INADDR_ANY or 0.0.0.0), which is returned as a sockaddr_in structure. It also makes sense to return the IPv6 wildcard address first because we will see in Section 12.2(See 9.1.2) that an IPv6 server socket can handle both IPv6 and IPv4 clients on a dual-stack host.
  • The address family specified in the hint structure’s ai_family member, along with the flags such as AI_V4MAPPED and AI_ALL specified in the ai_flags member, dictate the type of records that are searched for in the DNS (A and/or AAAA) and what type of addresses are returned (IPv4, IPv6, and/or IPv4-mapped IPv6). We summarize this in Figure 11.8.
  • The hostname can also be either an IPv6 hex string or an IPv4 dotted-decimal string. The validity of this string depends on the address family specified by the caller. An IPv6 hex string is not acceptable if AF_INET is specified, and an IPv4 dotted-decimal string is not acceptable if AF_INET6 is specified. But, if AF_UNSPEC is specified, either is acceptable and the appropriate type of socket address structure is returned.
‘getaddrinfo’ Function: Examples

We will now show some examples of getaddrinfo using a test program that lets us enter all the parameters: the hostname, service name, address family, socket type, and AI_CANONNAME and AI_PASSIVE flags. (We do not show this test program, as it is about 350 lines of uninteresting code. It is provided with the source code for the book, as described in the Preface.) The test program outputs information on the variable number of addrinfo structures that are returned, showing the arguments for a call to socket and the address in each socket address structure.

‘host_serv’ Function

Our first interface to getaddrinfo does not require the caller to allocate a hints structure and fill it in. Instead, the two fields of interest, the address family and the socket type, are arguments to our host_serv function.

#include "unp.h"
struct addrinfo *host_serv (const char *hostname, const char *service, int family, ints socktype);
#include "unp.h"struct addrinfo* host_serv(const char *host, const char *serv, int family, int socktype)
{int n;struct addrinfo hints, *res;bzero(&hints, sizeof(struct addrinfo));hints.ai_flags=AI_CANONNAME;hints.ai_family=family;hints.ai_socktype=socktype;if((n=getaddrinfo(host, serv, &hints, &res))!=0){return NULL;}return res;
}

host_serv function

这篇关于Unix Network Programming Episode 79的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/169949

相关文章

poj 2349 Arctic Network uva 10369(prim or kruscal最小生成树)

题目很麻烦,因为不熟悉最小生成树的算法调试了好久。 感觉网上的题目解释都没说得很清楚,不适合新手。自己写一个。 题意:给你点的坐标,然后两点间可以有两种方式来通信:第一种是卫星通信,第二种是无线电通信。 卫星通信:任何两个有卫星频道的点间都可以直接建立连接,与点间的距离无关; 无线电通信:两个点之间的距离不能超过D,无线电收发器的功率越大,D越大,越昂贵。 计算无线电收发器D

列举你能想到的UNIX信号,并说明信号用途

信号是一种软中断,是一种处理异步事件的方法。一般来说,操作系统都支持许多信号。尤其是UNIX,比较重要应用程序一般都会处理信号。 UNIX定义了许多信号,比如SIGINT表示中断字符信号,也就是Ctrl+C的信号,SIGBUS表示硬件故障的信号;SIGCHLD表示子进程状态改变信号;SIGKILL表示终止程序运行的信号,等等。信号量编程是UNIX下非常重要的一种技术。 Unix信号量也可以

图神经网络框架DGL实现Graph Attention Network (GAT)笔记

参考列表: [1]深入理解图注意力机制 [2]DGL官方学习教程一 ——基础操作&消息传递 [3]Cora数据集介绍+python读取 一、DGL实现GAT分类机器学习论文 程序摘自[1],该程序实现了利用图神经网络框架——DGL,实现图注意网络(GAT)。应用demo为对机器学习论文数据集——Cora,对论文所属类别进行分类。(下图摘自[3]) 1. 程序 Ubuntu:18.04

深度学习--对抗生成网络(GAN, Generative Adversarial Network)

对抗生成网络(GAN, Generative Adversarial Network)是一种深度学习模型,由Ian Goodfellow等人在2014年提出。GAN主要用于生成数据,通过两个神经网络相互对抗,来生成以假乱真的新数据。以下是对GAN的详细阐述,包括其概念、作用、核心要点、实现过程、代码实现和适用场景。 1. 概念 GAN由两个神经网络组成:生成器(Generator)和判别器(D

【unix高级编程系列】线程

引言 我们知道unix进程中可以有多个线程,进程中的线程可以访问该进程的所有组成部分。并且CPU的调度单元就是线程。这就面临一个问题:当进程中的临界资源需要在多个线程中共享时,如何解决一致性问题? 本文将从线程的概念、线程的使用方式、unix提供哪些方式解决一致性问题进行介绍,加深对线程的理解。 线程概念 线程的优点: 简化代码结构。比如在业务上为每种事件类型分配单独的处理线程,可以

Neighborhood Homophily-based Graph Convolutional Network

#paper/ccfB 推荐指数: #paper/⭐ #pp/图结构学习 流程 重定义同配性指标: N H i k = ∣ N ( i , k , c m a x ) ∣ ∣ N ( i , k ) ∣ with c m a x = arg ⁡ max ⁡ c ∈ [ 1 , C ] ∣ N ( i , k , c ) ∣ NH_i^k=\frac{|\mathcal{N}(i,k,c_{

F12抓包05:Network接口测试(抓包篡改请求)

课程大纲         使用线上接口测试网站演示操作,浏览器F12检查工具如何进行简单的接口测试:抓包、复制请求、篡改数据、发送新请求。         测试地址:https://httpbin.org/forms/post ① 抓包:鼠标右键打开“检查”工具(F12),tab导航选择“网络”(Network),输入前3项点击提交,可看到录制的请求和返回数据。

OpenSNN推文:神经网络(Neural Network)相关论文最新推荐(九月份)(一)

基于卷积神经网络的活动识别分析系统及应用 论文链接:oalib简介:  活动识别技术在智能家居、运动评估和社交等领域得到广泛应用。本文设计了一种基于卷积神经网络的活动识别分析与应用系统,通过分析基于Android搭建的前端采所集的三向加速度传感器数据,对用户的当前活动进行识别。实验表明活动识别准确率满足了应用需求。本文基于识别的活动进行卡路里消耗计算,根据用户具体的活动、时间以及体重计算出相应活

linux的nohup命令的用法。在应用Unix/Linux时,我们一般想让某个程序在后台运行,于是我们将常会用 在程序结尾来让程序自动运行。比如我们要运行mysql在后台: /usr/local

在应用Unix/Linux时,我们一般想让某个程序在后台运行,于是我们将常会用 & 在程序结尾来让程序自动运行。比如我们要运行mysql在后台: /usr/local/mysql/bin/mysqld_safe –user=mysql &。可是有很多程序并不想mysqld一样,这样我们就需要nohup命令,怎样使用nohup命令呢?这里讲解nohup命令的一些用法。 nohup /root/

deepcross network(DCN)算法 xdeepfm是DCN的进阶

揭秘 Deep & Cross : 如何自动构造高阶交叉特征 https://zhuanlan.zhihu.com/p/55234968 Deep & Cross Network总结 Deep和Cross不得不说的秘密 [深度模型] Deep & Cross Network (DCN) https://mp.weixin.qq.com/s/Xp_xTmcx56tJqfjMhFsArA