Thread: the listen() function

  1. #1
    Politics&Cpp geek Da-Nuka's Avatar
    Join Date
    Oct 2004
    Posts
    104

    the listen() function

    Is the listen() function usually blocking?
    And if I choose to unblock it, and do a WSAAsyncSelect on the used socket, how can I later continue the accept process?



    Or even shorter and simpler:
    How can I easily listen for a client, and connect to it, unblocking?

    (Im expecting just a quick and little example)

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    I haven't done any thing with Winsock (only a little bit with *nix sockets actually). Basicly with nonblocking sockets, you pretty much just need to keep checking if a client has requested, which means looping. I can't say any thing more then that, as I truthfully don't know where to got from there (heck that is probably a bit off any how), but it might start giving you an idea.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Is the listen() function usually blocking?
    No, it isn't.

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    >>And if I choose to unblock it, and do a WSAAsyncSelect on the used socket, how can I later continue the accept process?

    With async sockets, handle the FD_ACCEPT message, ie:
    Code:
    //set up async server socket
    if (WSAAsyncSelect(hSocket, hwnd, WM_WSAASYNC, FD_READ |  
                                            FD_ACCEPT | FD_CLOSE)!=0)
    {
      //handle error
    }
    sockaddr_in	sockAddr = {0};
    SetServerSockAddr(&sockAddr, SERVER_PORT);
    if (bind(hSocket, reinterpret_cast<sockaddr*>(&sockAddr), sizeof(sockAddr))!=0)
      //handle error
    if (listen(hSocket, SOMAXCONN)!=0) //set socket to listen for incoming data
      //handle error
    Code:
    //in message loop
     case FD_ACCEPT:
      if (WSAGETSELECTERROR(lParam))
      {
        MessageBox(NULL,"error accepting connection","FD_ACCEPT Error",MB_OK);
      }  
      //accept connection
    break;
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    listen() can only hold up to five requests in a queue, so if there are many requests at one time, listen() deny everybody but the first five, right? Do web servers, like Apache, use listen()?

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    No. Some BSD implementations limit the values passed to listen() to 5, but most *nix systems use the value SO_MAXCONN to determine the maximum listen value (128 on my implementation).

  7. #7
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    Is that a no to my first or second question?

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    That is a no to your comment that listen() can only hold up to five requests in a queue.

    Apache does use the listen function.

  9. #9
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    Thanks.

    I've heard that Apache can handle many incoming connections at the same time. How does it get around the limited queue size?

  10. #10
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    listen() doesn't put a cap on incoming connections, but rather puts a cap on sockets waiting to be accepted. A fast server would be hard pressed to let more than a couple sockets pile up in the queue, so it's not really a problem. Applications like apache, usually have memory pre-allocated for new connections as well, so it allows the program to accept new connections ever faster.

    Also, if the queue is full, and a new connection attempt fails (receiving ECONNREFUSED), it can just silently attempt a reconnect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  4. Brand new to C need favor
    By dontknowc in forum C Programming
    Replies: 5
    Last Post: 09-21-2007, 10:08 AM
  5. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM