How to determine the value of socket listen() backlog parameter?

1,037

Solution 1

From the docs:

A value for the backlog of SOMAXCONN is a special constant that instructs the underlying service provider responsible for socket s to set the length of the queue of pending connections to a maximum reasonable value.

Solution 2

There's a very long answer to this in the Winsock Programmer's FAQ. It details the standard setting, and the dynamic backlog feature added in a hotfix to NT 4.0.

Solution 3

I second using SOMAXCONN, unless you have a specific reason to use a short queue.

Keep in mind that if there is no room in the queue for a new connection, no RST will be sent, allowing the client to automatically continue trying to connect by retransmitting SYN.

Also, the backlog argument can have different meanings in different socket implementations.

  • In most it means the size of the half-open connection queue, in some it means the size of the completed connection queue.
  • In many implementations, the backlog argument will multiplied to yield a different queue length.
  • If a value is specified that is too large, all implementations will silently truncate the value to maximum queue length anyways.
Share:
1,037
whytheq
Author by

whytheq

Updated on January 25, 2020

Comments

  • whytheq
    whytheq over 4 years

    The following works fine for a windows form.

    It simply uses a delegate to reactivate a GroupBorder on the parent form when the child form is closed.

        private void uxUpdateDataButton_Click(object sender, EventArgs e)   
        {
            uxRevisionHelperGroupBox.Enabled = false;
            uxBindingNavigator.Hide();
            uxFormDatabase myNewDisplay = new uxFormDatabase();
            myNewDisplay.FormClosed += delegate { activateGroupBorder(); };
            myNewDisplay.Show();    
        }
    
        public void activateGroupBorder() 
        {
            uxRevisionHelperGroupBox.Enabled = true;
            uxBindingNavigator.Show();
        }
    

    Is it possible to do the equivalent to the above but for a messagebox instead of the child form?

    This is my attempt which is not the solution:

        private void uxAuthorPictureBox_Click(object sender, EventArgs e)
        { 
            uxRevisionHelperGroupBox.Enabled = false;
            uxBindingNavigator.Hide();
            MessageBox myMessage = new MessageBox;
            myMessage.close += delegate { activateGroupBorder(); };
            myMessage.Show("hello world"); 
        }
        public void activateGroupBorder()
        {
            uxRevisionHelperGroupBox.Enabled = true;
            uxBindingNavigator.Show();
        }     
    
    • loopedcode
      loopedcode almost 12 years
      MessageBox.Show(...) is synchronous call, i.e. execution doesn't continue further until box is closed. Why can't you just call your activateGroupBorder() after the message box is done? myMessage.Show("hello world"); this.activateGroupBorder();
    • whytheq
      whytheq almost 12 years
      +1 Cool - thanks. Why don't you put that as the solution?
    • whytheq
      whytheq almost 12 years
      I've added green tick :)
  • Roman Starkov
    Roman Starkov about 12 years
    Fantastic answer in that FAQ; thanks for sharing. Recommended.
  • Nyerguds
    Nyerguds about 11 years
    Terrible answer. What are such "reasonable values"?
  • spuriousdata
    spuriousdata almost 11 years
    I have to agree, this doesn't answer the question at all.
  • Kenton Varda
    Kenton Varda almost 8 years
    I disagree with the other comments. This is a perfectly good answer to the common question: "What the heck should I pass as the second parameter to listen()?" If you don't know, use SOMAXCONN.
  • tonysdg
    tonysdg about 6 years
    If you're on a Linux box, see the listen manpage: "If the backlog argument is greater than the value in /proc/sys/net/core/somaxconn, then it is silently truncated to that value; the default value in this file is 128. In kernels before 2.4.25, this limit was a hard coded value, SOMAXCONN, with the value 128."