- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
getaddrinfo接受 struct addrinfo *hints
作为第三个参数,可用于指定选择要由该函数返回的套接字地址的标准。
文档说我们可以设置 ai_socktype
以及 ai_protocol
来指定我们的选择标准。但是,如果我们已经指定了 ai_socktype
,我无法理解为什么需要 ai_protocol
。如果指定了这两者之一,那么另一个似乎是多余的。
这是我编写的一些代码来对此进行试验。
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
void getaddrinfo_demo(const char *node, const char *service,
int socktype, int protocol)
{
struct addrinfo hints, *res, *p;
int error;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = socktype;
hints.ai_protocol = protocol;
error = getaddrinfo(node, service, &hints, &res);
if (error) {
printf("Error %d: %s\n\n", error, gai_strerror(error));
return;
}
for (p = res; p != NULL; p = p->ai_next) {
struct sockaddr_in *addr = ((struct sockaddr_in *) p->ai_addr);
char ip[INET_ADDRSTRLEN];
int port = ntohs(addr->sin_port);
inet_ntop(AF_INET, &addr->sin_addr, ip, INET_ADDRSTRLEN);
printf("ip: %s; port: %d; protocol: %d\n", ip, port, p->ai_protocol);
}
printf("\n");
freeaddrinfo(res);
}
int main()
{
/* Consistent family and socktype works fine. */
getaddrinfo_demo("localhost", "http", SOCK_STREAM, IPPROTO_TCP);
getaddrinfo_demo("localhost", "http", SOCK_DGRAM, IPPROTO_UDP);
/* Inconsistent family and sock type leads to error -7. */
getaddrinfo_demo("localhost", "http", SOCK_STREAM, IPPROTO_UDP);
getaddrinfo_demo("localhost", "http", SOCK_DGRAM, IPPROTO_TCP);
}
这是输出。
$ gcc -std=c99 -D_POSIX_SOURCE -Wall -Wextra -pedantic foo.c && ./a.out
ip: 127.0.0.1; port: 80; protocol: 6
ip: 127.0.0.1; port: 80; protocol: 6
ip: 127.0.0.1; port: 80; protocol: 17
ip: 127.0.0.1; port: 80; protocol: 17
Error -7: ai_socktype not supported
Error -7: ai_socktype not supported
如您所见,如果 ai_socktype = AF_STREAM
,则只有 ai_protocol = IPPROTO_TCP
有效。指定 ai_protocol = IPPROTO_UDP
会导致错误。如果我们不能通过它指定任何额外的选择标准,那么也可以省略在 hints
中指定 ai_protocol
。
那么 ai_protocol
在 hints
中的真正作用是什么?您能否举例说明 ai_socktype
和 ai_protocol
都具有某种用途?
最佳答案
这些怎么样:
getaddrinfo_demo("localhost", "http", SOCK_STREAM, IPPROTO_SCTP);
getaddrinfo_demo("localhost", "imap", SOCK_STREAM, IPPROTO_TCP);
getaddrinfo_demo("localhost", "imap", SOCK_STREAM, IPPROTO_SCTP);
getaddrinfo_demo("localhost", "sbcap", SOCK_STREAM, IPPROTO_SCTP);
getaddrinfo_demo("localhost", "sbcap", SOCK_SEQPACKET, IPPROTO_SCTP);
getaddrinfo_demo("localhost", "sbcap", SOCK_STREAM, IPPROTO_TCP);
getaddrinfo_demo("localhost", "http", SOCK_DGRAM, IPPROTO_UDPLITE);
getaddrinfo_demo("localhost", "syslog-tls", SOCK_DCCP, IPPROTO_DCCP);
哪个给你:
ip: 127.0.0.1; port: 80; protocol: 132
ip: 127.0.0.1; port: 143; protocol: 6
Error -8: Servname not supported for ai_socktype
ip: 127.0.0.1; port: 29168; protocol: 132
ip: 127.0.0.1; port: 29168; protocol: 132
Error -8: Servname not supported for ai_socktype
Error -8: Servname not supported for ai_socktype
ip: 127.0.0.1; port: 6514; protocol: 33
所以 TCP 不是唯一的流协议(protocol),UDP 也不是唯一的数据报协议(protocol)(尽管 DCCP 有自己的 SOCK_DCCP
并且 UDP-Lite 在服务数据库中没有任何条目(有人可能会争辩说它不需要,因为 UDP-Lite RFC 明确表示“UDP-Lite 使用由 IANA 分配给 UDP 使用的同一组端口号值”)。
现在我们在实践中并不经常看到它,但是如果我们谈论像 getaddrinfo()
这样的 API,它们必须被设计成面向 future 的,这包括做一些看似多余的事情。只有时间才能证明这些东西是否真的是多余的。在这种情况下不是,恰恰相反。如果FreeBSD man page是正确的,那么
The implementation first appeared in WIDE Hydrangea IPv6 protocol stack kit.
仍然存在 FAQ关于互联网上的这个协议(protocol)栈,从中我们可以猜测它是在1997--1998年左右创建的(我还没有那么大的内存力,我也没有看到任何其他合适的来源,所以请纠正我'我错了)。和 SCTP was defined 2000 年。正如我在上面的示例中所展示的,我们使用此 API 的 SCTP 没有任何问题。 2005--2006 年左右出现的与 DCCP 相同的故事,非常适合相同的 API。
关于c - 当已经指定了 ai_socktype 时调用 getaddrinfo() 时 ai_protocol 可以在提示中起到什么额外的作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39474941/
我的 friend 编写了一个程序,它比较随机排列的骰子面,以找到分布最均匀的面——尤其是当面不仅仅是序列时。 我将他的程序翻译成 haskell 是因为我一直在寻找一个理由来让别人知道 haskel
我需要对表单中的某些字段进行评论/提示。我的想法是在模型中描述它,就像attributeLabels一样。我该怎么做? 然后它会是理想的,如果 Gii 模型(和 Crud)生成器直接从 mysql 列
我们使用 FastReport 来生成报告。事实上,我们为访问源代码付费。 我们目前使用的是 FastReport 的最新稳定版本。虽然它对于我们的生产来说足够稳定,但每当我编译时,我都会看到以下内容
我需要创建一个对话框/提示,包括用于用户输入的文本框。我的问题是,确认对话框后如何获取文本?通常我会为此创建一个类,将文本保存在属性中。不过我想使用 XAML 设计对话框。因此,我必须以某种方式扩展
我想提示用户是否要执行操作(删除) - 用警报框说"is"或“否”,如果是,则运行删除脚本,如果否,则不执行任何操作 我不太了解 javascript,因此是否有人可以使用 javascript 获得
所以我正在编写一个简单的 JS 代码。我们刚刚开始学习函数。我需要创建一个名为“printStars”的函数。 我需要从用户那里获取一个号码,并根据该号码打印“*”。 这就是我所做的:
我在我的页面上添加了一个提示,但它在页面加载之前加载了。如何仅在整个页面可见时才显示消息? 这是我的提示: if (name == null || name == "") { txt == "No
我在我的页面上添加了一个提示,但它在页面加载之前加载了。如何仅在整个页面可见时才显示消息? 这是我的提示: if (name == null || name == "") { txt == "No
我正在自定义我的 zsh 提示,并发现以下内容来检查是否有任何后台作业: if [[ $(jobs | wc -l) -gt 0 ]]; then # has background job(s)
这个问题在这里已经有了答案: JavaScript object: access variable property by name as string [duplicate] (3 个答案) pa
我正在尝试用 javascript 制作一个简单的数学练习程序。在提示警报中给出不同的值,并将答案与用户输入进行比较。这是代码: Calculations generate(); functio
在这段代码中,尽管我使用了文本对齐属性在“编辑文本” View 的中心设置“提示”。但它无法正常工作。 最佳答案 尝试 关于android - 如何在编辑文本的中心对齐文本(提示),我们在Sta
我正在尝试让我的 EditText 显示一个提示,例如“请在此处输入答案”,当用户点击 EditText 以键入他们的答案时,文本应该消失并留空,以便他们在其中输入答案. 截至目前,这就是我的 .xm
我当前的 android 应用程序中有两个微调器,我想要一个默认值,例如 editText 的 android:hint 功能。有没有办法这样做,但不会将提示添加到填充微调器的字符串数组。例如从微调器
如果我的表单已完全填写,我如何提示“感谢您填写表单,“name”!” function submit_onclick() { if(confirm("Thanks for completing t
我刚刚了解了prompt()命令;我知道 Prompt() 命令以字符串的形式返回用户输入。我正在搞乱下面的程序,我输入了Per“Dead”Ohlin作为男性名字。为什么这有效并且没有引起任何问题?
void openUpNow(FILE *x, FILE *y) { x = fopen("xwhatever", "r"); y = fopen("ywhatever", "r");
我有一个作业正在处理,但我在使用 prompt() 方法时遇到了问题。我看到我可以做一个提示,但我需要几个并且有数量。 例如... 我创建了一个 HTML 表格,其中包含许多艺术家和包含 DVD、CD
我正在学习 Big Nerd Ranch 的 iOS Programming, 2nd Edition,我已经来到第 4 章挑战:标题。该练习暗示我感到困惑;它说我需要做一些我认为不需要做的事情。 到
抱歉,如果这是微不足道的,但我没有找到任何解决此问题的建议。我在 Ubuntu 上,我的 Yii 项目需要 PHPUnit。我已经安装了 PHPUnit 两次,方法是下载 phpunit.phar 并
我是一名优秀的程序员,十分优秀!