2010-01-08 8 views
36

हाय मेरे पास यूडीपी सॉकेट के लिए प्रेषण बफर आकार की जांच करने के लिए निम्न प्रोग्राम है। हालांकि, मैं वापसी मूल्य मेरे लिए थोड़ा उलझन में है। मैं निम्नलिखित सरल अनुप्रयोग का उपयोग करें:सेट/होटॉकॉपट को समझना SO_SNDBUF

#include <sys/socket.h> 
#include <stdio.h> 

int main(int argc, char **argv) 
{ 
int sockfd, sendbuff; 
socklen_t optlen; 

sockfd = socket(AF_INET, SOCK_DGRAM, 0); 
if(sockfd == -1) 
    printf("Error"); 

int res = 0; 

// Get buffer size 
optlen = sizeof(sendbuff); 
res = getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &sendbuff, &optlen); 

if(res == -1) 
    printf("Error getsockopt one"); 
else 
    printf("send buffer size = %d\n", sendbuff); 

// Set buffer size 
sendbuff = 98304; 

printf("sets the send buffer to %d\n", sendbuff); 
res = setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &sendbuff, sizeof(sendbuff)); 

if(res == -1) 
    printf("Error setsockopt"); 


// Get buffer size 
optlen = sizeof(sendbuff); 
res = getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &sendbuff, &optlen); 

if(res == -1) 
    printf("Error getsockopt two"); 
else 
    printf("send buffer size = %d\n", sendbuff); 

return 0; 
} 

मेरी मशीन पर उत्पादन होता है:

भेजने बफर आकार = 129024

भेजने बफर आकार = 196,608

के लिए भेजें बफर सेट

क्या कोई यह स्पष्ट कर सकता है कि मैं यहां क्या गलत कर रहा हूं या आउटपुट की व्याख्या कैसे कर सकता हूं?

उत्तर

45

आप कुछ भी गलत नहीं कर रहे हैं। जब आप इसे सेट करते हैं तो लिनक्स मूल्य (कर्नेल के भीतर) को दोगुना करता है, और जब आप इसे पूछते हैं तो दोगुनी मान देता है। man 7 socket कहता है:

 
[...] 

    SO_SNDBUF 
       Sets or gets the maximum socket send buffer in bytes. The ker- 
       nel doubles this value (to allow space for bookkeeping overhead) 
       when it is set using setsockopt(), and this doubled value is 
       returned by getsockopt(). The default value is set by the 
       wmem_default sysctl and the maximum allowed value is set by the 
       wmem_max sysctl. The minimum (doubled) value for this option is 
       2048. 
[...] 

NOTES 
     Linux assumes that half of the send/receive buffer is used for internal 
     kernel structures; thus the sysctls are twice what can be observed on 
     the wire. 
[...] 
+9

पवित्र नेटवर्किंग बैटमैन! यही वह जगह है जहां सभी skbuf सामान जाता है :) –

+0

मुझे आश्चर्य है कि कर्नेल मूल्य क्यों दोगुना करता है? – csyangchen

+0

@csyangchen: मैं केवल अनुमान लगा सकता हूं, लेकिन मुझे लगता है कि किसी को यह विचार था कि बफर को आकार n पर सेट करके, बफर को PAYLOAD के एन बाइट्स को पकड़ने में सक्षम होना चाहिए। इसलिए संदेश शीर्षलेख रखने के लिए अतिरिक्त बफर आकार आवश्यक है (कम से कम कनेक्शन रहित अंतर्निहित प्रोटोकॉल के मामले में, जैसे कि यूडीपी)। – Aconcagua

संबंधित मुद्दे