c - 相当于 Windows 中的 TIOCOUTQ/SIOCOUTQ

标签 c sockets networking tcp ioctl

到目前为止,我的谷歌搜索和 msdn check out 表明 Microsoft Windows 不提供任何等效的 TIOCOUTQ/SIOCOUTQ 来查找套接字发送缓冲区中未发送的数据量。 如果一些优秀的 hack 发现了一些其他的东西,并且可以在这里发布,那将非常有用。 它将使我的以下代码成为跨平台的

int net_get_unsent (const int sockfd, unsigned long &sock_bytes_unsent )
{

/*  Queries how much data sent on sockfd, 
 *  may not have yet been recieved by the reciever
 * 
 *  returns more than 1 if there's unsent data,
 *  and fills in the amount of unsent data in sock_bytes_unsent
 * 
 * returns 0 if all the data has been sent
 * 
 * returns < 0 if there was a socket I/O error 
 * */

    int err = 0;
    int wsaerr = 0;

    if (sockfd <= 0)
        return -1;

#ifdef WINDOWS
    err = ioctlsocket(sockfd, SIOCOUTQ , &sock_bytes_unsent);

#else
    err = ioctl(sockfd, TIOCOUTQ, &sock_bytes_unsent);

#endif
    if (err < 0) {

#ifdef WINDOWS  
        wsaerr = WSAGetLastError();
#else
        wsaerr = errno;
#endif      
        printf("failed to get unsent data error:%d", wsaerr );
        sock_bytes_unsent = 0;
        return -1 * wsaerr;

    } else if (sock_bytes_unsent > 0 ) {
        printf( "unsent data: %lu bytes", sock_bytes_unsent );
        return 1;
    }

    return err;
}

最佳答案

据我所知,Windows 中没有这样的调用。 (很遗憾,因为它工作得很好!)

但是,我发现以下链接非常有用,可以帮助您解决问题。这些概念适用于任何操作系统,显然“还能做什么?”部分中提到的 SIOCOUTQ 部分除外。

The ultimate so_linger page or why is my tcp not reliable

关于c - 相当于 Windows 中的 TIOCOUTQ/SIOCOUTQ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12914168/

相关文章:

c - 如何在C中合并两个数组?

c++ - 错误 : invalid conversion from 'int' to 'std::_Ios_Openmode' |

c# - 如何确定网络接口(interface)是否连接到互联网(即计算机在线)

c++ - 建立SSL连接并发送GET信息

C - 段错误

c - 我想在 c51 (Keil) 的液晶显示器上打印一个多维字符数组

c - 在 Windows 10 系统上从 C 中的 getchar() 读取 EOF

java - 无法在不同方法中定义的内部类中引用非最终变量 str - 如何解决此问题?

java - 如何从Socket获取查询字符串?

swift - 回调/闭包如何知道在异步调用完成后等待?