首页 > 解决方案 > C中sendto()和recvfrom()之间的形式参数区别

问题描述

我正在使用recvfrom()andsendto()来接收和发送 UDP 数据包。

我注意到recvfrom()最后一个参数需要一个指向存储服务器地址长度的变量的指针,而sendto()需要存储客户端地址长度的变量

为什么会有这种差异?

标签: csocketsudpsendtorecvfrom

解决方案


我注意到 recvfrom() 作为最后一个参数需要一个指向存储服务器地址长度的变量的指针,而 sendto() 需要存储客户端地址长度的变量。

你实际上回答了你自己的问题

recvfrom() as last parameter requires a pointer to
the variable storing the length of server address.


while sendto() requires the variable that stores 
the length of the client address.

recvfrom()需要指针,因为它将值存储到该指针指向的位置。由于c没有所谓的引用调用,因此您需要传递指针来模拟引用调用行为。


推荐阅读