LINUX socket SIOCOUTQ获取发送队列信息

在无线网络拥塞时,TCP socket的发送可能会延迟。Linux提供了ioctl(SIOCOUTQ)接口用于查询socket的发送队列,帮助发送方判断接收方是否收到数据。然而,该方法并不完全可靠,因为ACK丢失可能导致发送队列显示未清空,可能导致程序死锁。在设计同步状态机时,应谨慎使用SIOCOUTQ。
摘要由CSDN通过智能技术生成

在某C/S结构程序测试时发现一个问题

当无线网络拥塞的时候,无线层丢包很严重,TCP socket可能需要若干秒(worst case n>5)才能发出去。

发送方调用write(fd)将报文发送的时候实际上只是写入了内核的write buffer。接收方什么时候能收到报文是个未知数。

在某些需要同步状态机的地方,发送方最好能够确认接收方收到报文后再进行下一步动作。

C: How to tell the status of a socket
by George Michael|Published 30 October 2011
If you are trying to check if a socket is empty but you cannot use select(), because what you need is to check the write buffers instead of the read ones just to be sure that you have successfully managed to send all data to your peers before terminating, or if you want to tell if a socket buffer is full.

You can use the ioctl to find out.

To check a write buffer if it empty (assuming you have already put data there and want to check if they were consumed):

ioctl(fd, SIOCOUTQ, &pending);
Where fd is the socket’s file descriptor and pending the variable were the remaining size of data will be returned.

To check a read buf
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值