- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我试图理解 sys/socket.h 的 msghdr
结构的以下成员图书馆。
struct iovec *msg_iov 分散/聚集数组
void *msg_control 辅助数据,见下文
内容如下:
Ancillary data consists of a sequence of pairs, each consisting of a cmsghdr structure followed by a data array. The data array contains the ancillary data message, and the cmsghdr structure contains descriptive information that allows an application to correctly parse the data.
我假设 msghdr
结构包含协议(protocol)头信息?如果是这样... *msg_iov
是请求/响应中参数的输入/输出“vector ”吗? *msg_control
包含响应消息?
最佳答案
msg_iov
是长度为 msg_iovlen
的输入/输出缓冲区数组.该数组的每个成员都包含一个指向数据缓冲区的指针和缓冲区的大小。这是要读取/写入的数据所在的位置。它允许您读取/写入不一定位于连续内存区域中的缓冲区数组。
msg_control
指向大小为 msg_controllen
的缓冲区包含有关数据包的附加信息。要读取这个字段,首先需要声明一个struct cmsghdr *
(我们称之为 cmhdr
)。您可以通过调用 CMSG_FIRSTHDR()
来填充它第一次,将 msghdr
的地址传递给它结构,和 CMSG_NXTHDR()
随后每次,将 msghdr
的地址传递给它结构和 cmhdr
的当前值.
来自msg_control
,您可以找到一些有趣的信息,例如数据包的目标 IP(对多播有用)和 IP header 中 TOS/DSCP 字节的内容(对自定义拥塞控制协议(protocol)有用)等。在大多数情况下,您需要制作 setsockopt
调用以启用接收此数据。在给出的示例中,IP_PKTINFO
和 IP_TOS
需要启用选项。
参见 cmsg(3) manpage了解更多详情。
源IP和端口,不在msg_control
中, 但在 msg_name
它需要一个指向 struct sockaddr
的指针长度msg_namelen
.
这是一个如何使用它的例子:
struct msghdr mhdr;
struct iovec iov[1];
struct cmsghdr *cmhdr;
char control[1000];
struct sockaddr_in sin;
char databuf[1500];
unsigned char tos;
mhdr.msg_name = &sin
mhdr.msg_namelen = sizeof(sin);
mhdr.msg_iov = iov;
mhdr.msg_iovlen = 1;
mhdr.msg_control = &control;
mhdr.msg_controllen = sizeof(control);
iov[0].iov_base = databuf;
iov[0].iov_len = sizeof(databuf);
memset(databuf, 0, sizeof(databuf));
if ((*len = recvmsg(sock, &mhdr, 0)) == -1) {
perror("error on recvmsg");
exit(1);
} else {
cmhdr = CMSG_FIRSTHDR(&mhdr);
while (cmhdr) {
if (cmhdr->cmsg_level == IPPROTO_IP && cmhdr->cmsg_type == IP_TOS) {
// read the TOS byte in the IP header
tos = ((unsigned char *)CMSG_DATA(cmhdr))[0];
}
cmhdr = CMSG_NXTHDR(&mhdr, cmhdr);
}
printf("data read: %s, tos byte = %02X\n", databuf, tos);
}
关于c - 从 sys/socket.h 了解 msghdr 结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32593697/
我正在编写一个 python 程序,它将所有输入都大写(替代非工作 tr '[:lowers:]' '[:upper:]')。语言环境是 ru_RU.UTF-8,我使用 PYTHONIOENCODIN
我收到错误;MVC Microsoft JScript 运行时错误:Sys.ArgumentTypeException:“Sys._Application”类型的对象无法转换为“Sys._Applic
尝试使用 IDLE 在 Python 3.7.4 版本中安装 sys 包时,出现以下错误: 输入:C:\Users\UserName\Downloads>pip install sys 输出: 采集系
我对 Python 还很陌生,所以我还在学习这门语言。我遇到的一件事是重新分配 sys.stdout 以更改打印的默认输出。所以我写了这个作为测试: import sys sys.stdout = o
我应该选择 sys.exc_info()在 sys.last_value和 friend (sys.last_type,sys.last_traceback)? 最佳答案 查看sys.last_val
我的 shell 脚本中出现奇怪的错误。使用这个: $find /sys/class/ -name temp -exec cat '{}' ';' 我得到输出 77000 find: `/sys/ke
我只想从 sys 库导入 argv 和可执行函数。 所以我使用 from 和 import 来指定这些函数,如下所示:- from sys import argv from sys import ex
我想找到调用我的程序的命令行参数,即 sys.argv,但我想在 Python 使 sys.argv 可用之前执行此操作.这是因为我在 usercustomize.py 中运行代码,该代码由 site
sys和os.sys在python中有什么区别?我见过很多项目在导入os时使用sys。当我尝试 dir(sys) 和 dir(os.sys) 时,它们的功能相同,输出也相同。 我经常看到像这样使用 s
create table T ( ID number, COL1 SYS.XMLTYPE )XMLType COLUMN COL1 STORE AS CLOB; select obj#,col
下面的 SQL 似乎可以工作,但我想知道是否有更好的方法来编写它。我正在尝试选择具有特定名称的所有 View 和表。如果找到匹配项,则应该只有一个返回值。 SELECT DISTINCT name F
在我的客户端(使用 LWJGL)中,我使用以下代码: private static long getTime() { return (Sys.getTime() * 1000) / Sys.g
我的老板让我看一些旧代码,其中所有内容都被发送到 stderr。我知道 stderr 应该有警告和错误,但他们什么时候才真正应该转到 stdout? 此程序是一项服务。它发送到 stderr 的一些消
使用 importlib,“Meta Path Finder”(通过遍历 sys.meta_path 找到)和“Path Entry Finder”(通过遍历 sys.path_hooks 找到)有什
我正在运行这个命令来安装 os_sys 包: pip3 install os_sys 但收到此错误: ERROR: os-sys has an invalid wheel, could not rea
如果我查询sys.dm_database_encryption_keys,它返回的encryption_state为3(加密),percent_complete 0。如果我查询 sys.databas
有人可以向我解释一下这些不同的环境函数具体有什么作用吗?即哪个返回什么帧?阅读文档后我完全困惑了(http://stat.ethz.ch/R-manual/R-patched/library/base
查看其他 stackoverflow 帖子后,我似乎无法解决这个重定向问题。我想做的是抑制 stdout 和 stderr,然后在捕获错误后恢复它们。抑制效果很好,但恢复它们只成功了一半。 如果我尝试
这个问题在这里已经有了答案: Understanding slicing (38 个答案) 关闭 3 个月前。 我写了这段代码: #!/usr/bin/env python import sys i
我正在创建一个名为 Qt ( Github link ) 的模块,我在其中为另一个模块起别名(例如 PyQt4),这样当我导入 Qt 我实际上正在导入 PyQt4: from Qt import Qt
我是一名优秀的程序员,十分优秀!