How select() works

This is a discussion on How select() works within the Networking/Device Communication forums, part of the General Programming Boards category; First I know it's not a good idea to non-block a socket. Second, how can a non-blocking connect() work, i'm ...

  1. #16
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    First I know it's not a good idea to non-block a socket.

    Second, how can a non-blocking connect() work, i'm not writing a program for a lan network with replies under 1ms... The times over the internet are pretty high, if i non-block connect() it will simply never connect!
    "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.

  2. #17
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    Ok, you've all given me what select() does. I'm pretty comfortable with monitoring file descriptors for reading: whenever someone writes on a socket, select returns, and you can check what socket was written to and then perform a read. But I still cannot think of an example of when I would monitor sockets for writing. All the code I've seen on the Web monitors sockets for reading, so I would very much appreciate it if someone could give me an example along with code of when I would monitor sockets for writing.

    Thanks,
    Yasir

  3. #18
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,538
    Try sending a large file somewhere, and see how long each successive call to send() takes.

    Before long, you'll see send() taking a lot longer than it used to (because all the interal buffers are full), and you have to wait around for them to clear.
    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.

  4. #19
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    Quote Originally Posted by Devil Panther
    First I know it's not a good idea to non-block a socket.

    Second, how can a non-blocking connect() work, i'm not writing a program for a lan network with replies under 1ms... The times over the internet are pretty high, if i non-block connect() it will simply never connect!
    anyway is there a way to change the default timeout value of connect(), without changing the socket to non-blocking...
    Last edited by Devil Panther; 03-12-2005 at 02:22 AM.
    "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.

  5. #20
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    Quote Originally Posted by Salem
    Before long, you'll see send() taking a lot longer than it used to (because all the interal buffers are full), and you have to wait around for them to clear.
    Ok, so monitoring write file descriptors are used to see when the internal buffers are empty so you can start writing again?

  6. #21
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    Quote Originally Posted by Devil Panther
    anyway is there a way to change the default timeout value of connect(), without changing the socket to non-blocking...
    So want to time out if you can't connect() to a server, right? You could set an alarm(2) and then after the alarm goes off, handle it through signal(2). I've used this approach a long time ago in the client process to see if the server has gone down while the client is waiting for a response. I can't remember how I did it, though.

  7. #22

  8. #23
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    As I was implementing what I was suggested to you, I found out that connect() returns immediately when there isn't someone to connect to. I thought it would wait. alarm and signal won't work then.

  9. #24
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    the problem that it doesn't return immediately!
    infact i have to wait a whole minute for it to timeout, and it's killing me.
    which is the default timeout value for this function.
    "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.

  10. #25
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    connect() doesn't return immediately? It does on my machine. But if that's what's happening on your machine, use the signal() alarm() combination I suggested and look at the links I posted.

  11. #26
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    are you saying i can interupt the connectcall in the middle with that???
    "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. #27
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    I think so. Try it. Set up a signal with SIG_ALARM and call alarm with some number of seconds. Set up your setjmp calls, and in the signal function handler use longjmp() to jump back to the place you called setjmp(). The key is that setjmp initially returns 0, but when you call longjmp(), it returns what you passed in longjmp()'s second parameter.

  13. #28
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    hmmm since connect blocks, the program "stops" on that call to connect. I'm guessting i need to use more than one thread / process to do that, no?
    "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.

  14. #29
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    What don't you like about my solution? Have you tried it? I would like to try it, but on my machine, connect() returns immediately when there isn't a server to connect to.
    Last edited by Yasir_Malik; 03-31-2005 at 03:54 PM. Reason: Adverb follows always the verb

  15. #30
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Timing out connect():
    unix network programming
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Page 2 of 3 FirstFirst 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, 12:45 PM
  2. select and EINTR
    By mynickmynick in forum C Programming
    Replies: 6
    Last Post: 07-29-2008, 05: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, 12:50 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

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