本文介绍了UDP(数据报)套接字的 FIONREAD 返回什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

FIONREADioctl 返回的是下一个数据包的长度,还是缓冲区中所有数据的长度?

假设有一个 UDP 服务器从客户端 1 接收 2 个数据包,并在客户端 1 的数据包之后从客户端 2 接收另外 2 个数据包.那么,FIONREADioctl的值是多少,在这种情况下 readfrom 返回什么?

客户端 1:v 两个数据包

++UDP 服务器得到 4 个数据包 <- FIONREAD?

客户端 2 : ^ 两个数据包

FIONREAD?(服务器)

  1. 客户端 1 的第一个数据包的长度
  2. 客户端 1 的两个数据包的长度
  3. 客户端1的两个包+客户端2的两个包的长度
  4. 客户端1的第一个数据包长度+客户端2的第一个数据包
  5. 其他
解决方案

man udp (7) 状态:

 FIONREAD (SIOCINQ)获取一个指向整数的指针作为参数.返回大小以字节为单位的整数中的下一个待处理数据报,如果没有,则为0数据报未决.警告:使用 FIONREAD,这是不可能的区分没有数据报未决的情况下一个未决数据报包含零字节的情况数据.使用 select(2)、poll(2) 或 epoll(7) 更安全区分这些情况.

因此,您的问题的答案是:FIONREAD 返回下一个(第一个)待处理数据报的大小.

Which one does ioctl of FIONREAD return, the next packet's length, or the length of all data in the buffer?

Suppose there is a UDP server that receives 2 packets from a client 1 and another 2 packets from client 2 after client 1's packet.Then, what is the value of ioctl of FIONREAD,and what does readfrom return in that case?

Client 1 : v two packets

++UDP Server got 4 packets <- FIONREAD?

Client 2 : ^ two packets

FIONREAD? (server)

  1. length of client 1's first packet
  2. length of client 1's two packets
  3. length of client 1's two packets + client 2's two packets
  4. length of client 1's first packet + client 2's first packet
  5. other
解决方案

man udp (7) states:

So, the answer to your question is: FIONREAD returns the size of the next (first) pending datagram.

这篇关于UDP(数据报)套接字的 FIONREAD 返回什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-16 21:34
查看更多