- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
size_t recvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen)
src_addr
参数以什么字节顺序写入?网络还是主机?我无法在 recvfrom
手册页或通过 google 和 SO 进行搜索时找到它。
最佳答案
假设套接字是 IPv4 或 IPv6 套接字,src_addr
中存储的主机和端口将按网络字节顺序排列。
这记录在 man page for IPv4 (man 7 ip
) 中如下:
Address format
An IP socket address is defined as a combination of an IP interface address and a 16-bit port number. The basic IP protocol does not supply port numbers, they are implemented by higher level protocols
like udp(7) and tcp(7). On raw sockets sin_port is set to the IP protocol.struct sockaddr_in {
sa_family_t sin_family; /* address family: AF_INET */
in_port_t sin_port; /* port in network byte order */
struct in_addr sin_addr; /* internet address */
};
/* Internet address. */
struct in_addr {
uint32_t s_addr; /* address in network byte order */
};
sin_family
is always set toAF_INET
. This is required; in Linux 2.2 most networking functions returnEINVAL
when this setting is missing.sin_port
contains the port in network byte order. The port numbers below 1024 are called privileged ports (or sometimes: reserved ports). Only a privileged process (on Linux: a process that has the CAP_NET_BIND_SERVICE capability in the user namespace governing its network namespace) may bind(2) to these sockets. Note that the raw IPv4 protocol as such has no concept of a port, they are implemented only by higher protocols like tcp(7) and udp(7).
sin_addr
is the IP host address. Thes_addr
member of structin_addr
contains the host interface address in network byte order.in_addr
should be assigned one of theINADDR_*
values (e.g.,INADDR_LOOPBACK
) using htonl(3) or set using the inet_aton(3), inet_addr(3), inet_makeaddr(3) library functions or directly with the name resolver (see gethostbyname(3)).
ipv6 手册页有类似的措辞。
所以在读取端口号的时候,使用ntohs
来提取。读取地址时,使用inet_ntop
将其转换为文本形式。
关于c - recvfrom() 将 src_addr 保存为什么字节顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55084142/
我目前正在编写一个供个人使用的小型 UDP 套接字库,但我一直无法找到这方面的任何文档。 我指的函数是ssize_t recvfrom(int sockfd, void *buf, size_t le
size_t recvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *
我正在编写一个通过 unix 域套接字监听 UDP 数据包的应用程序。考虑以下代码块。 int sockfd; struct sockaddr_un servaddr; sockfd = socket
我是一名优秀的程序员,十分优秀!