在某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