2012-10-16 20 views
3

截止日期我的搜索結果和檢出msdn建議Microsoft Windows不提供任何TIOCOUTQ/SIOCOUTQ的等價物來查找套接字發送緩衝區中未發送數據的數量。 如果一些好的黑客發現了別的東西,它會非常有用,並且可以在這裏發佈。 它將使我下面的代碼,成爲跨平臺相當於windows中的TIOCOUTQ/SIOCOUTQ

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; 
} 
+0

相關:http://stackoverflow.com/q/12858259 – icktoofay

回答

1

據我知道有沒有在Windows這樣的稱呼。 (可惜,因爲它工作得很好!)

但是,我發現以下鏈接非常有用,可以幫助您解決問題。這些概念適用於任何操作系統,除了明顯是中提到的SIOCOUTQ部分「還能做什麼?」。部分。

The ultimate so_linger page or why is my tcp not reliable

+0

僅供參考,該函數在depleteSendBuffer [SIOCOUTQ示例代碼(http://ds9a.nl/tcp-programs.tar.gz)在那篇文章中引用的內容非常糟糕,如果在確認所有數據之前進行遠程異常終止連接,將會顯着死鎖,從而阻止您的SIOCOUTQ值達到0。 – Pavel