c - recvfrom() 会修改 len 吗?

标签 c linux ip packet

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);

最佳答案

quote the man page

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 the address argument, and the length of this address shall be stored in the object pointed to by the address_len argument.

因此,它是按状态设计的,recvfrom() 将修改最后一个参数指向的值(前提是)

  • address 参数不是空指针
  • 协议(protocol)提供消息的源地址

关于c - recvfrom() 会修改 len 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45172698/

相关文章:

android - (android)如何判断android热点中已连接设备的ip地址

c - fgets 被跳过/不停止输入

php - apache 通过 ftp 的配置问题

linux - 在 make 文件中正常分配和导出分配之间的区别

javascript - IsInNet for ASP

php - 通过IP地址和时间间隔限制表单提交速率

在 C 中创建一个带有字符的字符串数组

c - 操作系统是用什么C写的?

C - 优先级递增/递减?

c - linux内核中的中断