博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LINUX socket SIOCOUTQ获取发送队列信息
阅读量:4210 次
发布时间:2019-05-26

本文共 1582 字,大约阅读时间需要 5 分钟。

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

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

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

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

C: How to tell the status of a socketby George Michael|Published 30 October 2011If 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 buffer if it empty (assuming someone has already put data there and you want to check if they there is any without consuming them):ioctl(fd, SIOCINQ, &pending);*note the difference on the second parameter, where we change the flag from SIOCOUTQ to SIOCINQAmong other includes be sure to add these:#include 
#include

linux提供了ioctl(fd, SIOCOUTQ, &count)方法来查询一个tcp socket的write buffer是否清空。发送方一般可以用这个方法来判断对端是否收到报文。

调试程序的时候遇到了一个奇怪的问题。

CLIENT端实际上已经收到了SERVER发送的报文,开始进行下一步动作。 但是SERVER端的out queue始终显示没有发送完毕。

经过Debug后发现,实际上是client端收到报文后发送给server的TCP ACK在空口中丢失。尽管client端已经接收完毕数据,但是由于server端没有收到对应的ack,导致server端一直认为发送端没有收到数据。

所以发送方可以通过SIOCOUTQ来辅助判断tcp socket的状态。但是最好还是不要用SIOCOUTQ来绑定程序逻辑。不然容易造成死锁。

转载地址:http://pwrli.baihongyu.com/

你可能感兴趣的文章
根据身份证号码来提取人员的信息【身份证号码的前六位所代表的省,市,区, 以及地区编码】的网上地址
查看>>
php安装工具 网址
查看>>
php NetBeans IDE Build 201208070001 打开一个现有的php 网站
查看>>
win7系统中, Microsoft Office Word已停止工作
查看>>
.net中 网页抓取数据(提取html中的数据,提取table中的数据)
查看>>
c# Windows Forms Application中的DataGridView的数据指定列绑定 简单小例子
查看>>
c#中 Word中的回车键
查看>>
sql中取出字符串中数字
查看>>
js下拉列表框的联动事件
查看>>
在sql中获取字符串中的数字的函数
查看>>
gridview多行footer,并且合并footer单元格
查看>>
Linq 查询 网址:
查看>>
※默认电脑系统中的小图片的路径及※默认我的电脑桌面的背景图片的路径
查看>>
ext的学习网址
查看>>
学习的网址:j-query的学习网址:
查看>>
sql脚本查询所有数据库表名
查看>>
IEnumerable的应用(即:对于所有数组的遍历,都来自IEnumerable)
查看>>
CSS-用户控件在实际应用页面的div的css
查看>>
js表格求和的方法
查看>>
T4MVC的其中解决bug的方法
查看>>