Thread: sockets and timeouts

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    58

    sockets and timeouts

    We have this select statement

    rc = select(fd+1, &fds, NULL, NULL, &tv);


    now the timeout variable tv, say for example is set to 5.

    Does this mean that this socket will timeout if no data is sent or received within the 5 secs?

    Basically what i am trying to do is this:
    I have a client/server, and the client will send info to server, and if server doesnt respond in 5 secs i want it to disconnect, this will be non blocking too

    any ideas please?

  2. #2
    Registered User
    Join Date
    Aug 2009
    Posts
    58
    Sorry i probably wasnt clear in this question so will try again.

    Using non blocking sockets.

    If i have a client and server.
    The client sends data to the server.

    because the server is non blocking also, if i use the select command does that mean that no other client can talk to the server till its done with the select?


    Whats my other options in getting the server to read data from more than one socket?

    Thanks

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Does this section from the manpage answer your first question?
    Code:
           timeout is an upper bound on the amount of time elapsed before select()
           returns.  It may be zero, causing select() to return immediately. (This
           is useful for polling.) If timeout is NULL (no timeout),  select()  can
           block indefinitely.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    58
    So if i have a clietn/server both non blocking and i want to send data across i should not use the select option on the server side???

    I write some rough code here but hope you get what i mean as i dont have the code with me
    Start with server
    Code:
    accept()
    /*will do infinite loop
    while(1)
    {
          read(sockfd, buffer, sizeof(buffer));
          printf("Message is %s" buffer);
    }
    so if i start this server up and connect two clients will this loop catch all the data on the socket from the two clients?

    Thanks

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I don't know too much about this sort of thing, but read() only reads from one file descriptor. I'd imagine you'd have to do something like this:
    Code:
    repeat indefinitely:
        call accept() to receive incoming connections
        if any incoming connections were received, add the file descriptor to a set of descriptors
        call select(), giving it the set of file descriptors with a timeout of a few milliseconds or whatever
        call read() on the sockets which are ready for reading
        close any sockets which have been disconnected
    The trouble is that you presumably want to receive incoming client connections even while other connections are not responding; so you need to call select() with a small (or zero) timeout. You'll have to have some other way of figuring out when each connection was last active.

    As I said, though, I really don't know much about this. Read the man pages ("man 2 select", etc) and Beej's tutorial (google it, you'll find it).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Implementing timeouts with TCP sockets
    By nkhambal in forum C Programming
    Replies: 2
    Last Post: 04-13-2005, 11:10 PM