sendto() 给我“无效参数”,因为 len 从 20 减少了 2 个字节。 为什么调用recvfrom()会修改len?
s=socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
struct sockaddr_ll sll;
len=sizeof(sll);
bzero((char *)&sll, sizeof(sll));
sll.sll_ifindex=3;
rf=recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr *) &sll, &len);
st=sendto(s, buf, 6+6+2+20+8+8, 0, (struct sockaddr *) &sll, len);
最佳答案
Syntax:
ssize_t recvfrom(int socket, void *restrict buffer, size_t length, int flags, struct sockaddr *restrict address, socklen_t *restrict address_len);
并且,
[...] If the
address
argument is not a null pointer and the protocol provides the source address of messages, the source address of the received message shall be stored in the sockaddr structure pointed to by theaddress
argument, and the length of this address shall be stored in the object pointed to by theaddress_len
argument.
因此,它是按状态设计的,recvfrom()
将修改最后一个参数指向的值(前提是)
address
参数不是空指针- 协议(protocol)提供消息的源地址
关于c - recvfrom() 会修改 len 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45172698/