How select() works

This is a discussion on How select() works within the Networking/Device Communication forums, part of the General Programming Boards category; Hello, Can someone please explain to me how select() works? I don't understand how it handles multiple sockets. In fact, ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    224

    How select() works

    Hello,
    Can someone please explain to me how select() works? I don't understand how it handles multiple sockets. In fact, I don't understand how multiple clients can log onto to a server that only uses select() to handle the clients.

    This is in relation to my DNS Message Format topic.
    Thanks,
    Yasir

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    http://www.ecst.csuchico.edu/~beej/g...ed.html#select

    Basically you setup a list of sockets to monitor. you then call select with this list as a parameter. select() simply checks to see if its avialable to be written to or read from depending on which parameter it is. If it is, it modifies the list and checks the next socket in the list.

    When select is done you then step through the list and check to see if a particular socket is avialable and if it is you do whatever operator you wanna do.

  3. #3
    the lowly newb
    Join Date
    Jan 2005
    Location
    IL
    Posts
    49
    is select non-blocking, and does it work in windows?
    "Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life."

    "They say a little knowledge is a dangerous thing, but it's not one half so bad as a lot of ignorance."

    "Light thinks it travels faster than anything but it is wrong. No matter how fast light travels, it finds the darkness has always got there first, and is waiting for it."
    [all]- Terry Pratchett

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    select has a time out parameter. So technically it is blocking while its executing you can setup a low time out so it doesn't block for too long

    It does work in windows.

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    *But there's better, more elegant methods available on Windows - select() is essentially there for portability.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    > It does work in windows.
    But it only works on socket descriptors.
    The Unix/Linux version of select works on any descriptor (eg. stdin)
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    the lowly newb
    Join Date
    Jan 2005
    Location
    IL
    Posts
    49
    ah, ok; then would it be better to use asynchronous sockets winsock. if so, could someone send me a link to asynchonous winsock sockets. i read those beej tutorials, but he seems to concentrate on linux so no winsock.

    thanks
    "Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life."

    "They say a little knowledge is a dangerous thing, but it's not one half so bad as a lot of ignorance."

    "Light thinks it travels faster than anything but it is wrong. No matter how fast light travels, it finds the darkness has always got there first, and is waiting for it."
    [all]- Terry Pratchett

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Look at the links page at the top of the forum.

  9. #9
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    I've used select() to monitor file descriptors ready for reading. I can't think of an example where I would monitor sockets ready for writing. Can someone please give me an example?

    Thanks,
    Yasir

  10. #10
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    When using non-blocking sockets, you'd monitor via select() or whatever, because there's no point in looping waiting for send() not to fail with EWOULDBLOCK.

  11. #11
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    select has a time out parameter. So technically it is blocking while its executing you can setup a low time out so it doesn't block for too long

    Can someone please post some code on how to setup the timeout value?

    Thank you.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  12. #12
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    Beej gives great examples on how to use the timout parameter in slect() -- look in his site.

  13. #13
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    man select

  14. #14
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    you were talking only about the select() timeout?!

    I thought you were talking about sockets in general, because I really need to find a way to set a timeout for connect()... that default one minute is killing me.

    oh well, thanks anyway.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  15. #15
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    Make the socket non-blocking?

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem in select() in a Multiprotocol client
    By zealot in forum Networking/Device Communication
    Replies: 2
    Last Post: 04-09-2009, 01:45 PM
  2. select and EINTR
    By mynickmynick in forum C Programming
    Replies: 6
    Last Post: 07-29-2008, 06:03 AM
  3. What would I use in place of select()
    By Overworked_PhD in forum Linux Programming
    Replies: 3
    Last Post: 06-21-2008, 01:50 PM
  4. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 05:42 PM
  5. scandir select function
    By dsl24 in forum C Programming
    Replies: 3
    Last Post: 04-12-2002, 11:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21