Thread: select() question (winsock2).

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    80

    select() question (winsock2).

    Hello everyone!
    In all tutorials I've read this far select is mainly used with non-blocking sockets. What I'm curious about is if there is something wrong about using select with blocking sockets like this:
    Code:
    timeval timer = {5, 0}; //wait five sec
    
    fd_set my_set;
    FD_ZERO(&my_set);
    
    SOCKET server = socket(AF_INET, SOCK_STREAM, 0);
    bind(server,..blah..blah..);
    listen(server, 5);
    
    FD_SET(server, &my_set);
    
    if (select(0, &my_set, NULL, NULL, &timer) == 1)
    {
        SOCKET client = accept(server, ...blah....blah...);
    }
    else
    {
        cout << "No connection within timelimit.";
    }
    This way I can await connections within a timelimit but still use blocking sockets and that's what I want to do, so is there anything bad about using select this way?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I don't see any problem with this.

    The blocking (or lack of) should only become a factor when you're actually reading and writing bytes of data.
    select() just monitors the state.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    80
    Okey thank you very much for answering! I can't imagine there's a better forum on the net
    Hopefully I'll be able to help people soon enough (as my knowledge grows) and not just ask questions without giving much in return.

    Thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  3. Question on Select Case?
    By Wescb in forum C++ Programming
    Replies: 8
    Last Post: 12-11-2002, 11:11 PM
  4. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM
  5. scandir select function
    By dsl24 in forum C Programming
    Replies: 3
    Last Post: 04-12-2002, 10:58 AM