recvfrom的addrlen参数

昨天用recvfrom去接收数据,好久没有过这个函数,按照man的手册,一个一个传参,但是遇到一个问题,数据可以接收正常,但是总是errno一直为22,并且sendto一直失败返回-1。这里做一下记录

函数原型:

ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags,
                        struct sockaddr *src_addr, socklen_t *addrlen);

前面几个参数没什么好说,一搜一大把。遇到的问题就在于最后一个参数:socklen_t *addrlen。看到传入指针,我的第一反应就是,这是一个结果参数,所以初始化完之后直接传入。导致在之后调用sendto时,一直失败。后来才发现recvfrom的函数说明中有下面一段话:

recvfrom()

recvfrom() places the received message into the buffer buf. The caller must specify the size of the buffer inlen.

If src_addr is not NULL, and the underlying protocol provides the source address of the message, that source address is placed in the buffer pointed to by src_addr. In this case, addrlen is a value-result argument. Before the call, it should be initialized to the size of the buffer associated with src_addr. Upon return, addrlen is updated to contain the actual size of the source address. The returned address is truncated if the buffer provided is too small; in this case, addrlen will return a value greater than was supplied to the call.

If the caller is not interested in the source address, src_addr and addrlen should be specified as NULL.

真神奇,粗心大意浪费了时间,打自己一下。。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值