Thread: how to include a "delay" in n/w progs

  1. #1
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342

    how to include a "delay" in n/w progs

    hi all,
    I wrote a client -server chatting program in C. I work on linux .
    The problem is : how do I make the server wait for the client to respond .
    I have used the select() function and set the tv parameter to some appropriate value. I have read the BEEJ's GUIDE ,but I could not clearly comprehend the exact funcion of the seletct() procedure...He has explained how to juggle around with multiple ports,but does not go in depth into "delay" aspects..
    Can anyone help me on this by sending pieces of code or may be directing me to an online resource which explains the select() procedure with examples

    thanks..

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    The select function tests for read/write/error conditions on file descriptors. Here's an example that waits 10 seconds for you to enter some data via STDIN, before giving up:
    (its from the faq)
    Code:
    #include <sys/types.h>
    #include <sys/time.h>
    #include <unistd.h>
    #include <stdio.h>
    
    int main(void)
    {
      fd_set          f;
      int             rc;
      char            buf[128];
      struct timeval  t;
    
    t.tv_sec = 10;
      t.tv_usec = 0;
    
      /* set descriptor */
      FD_ZERO(&f);
      FD_SET(STDIN_FILENO, &f);
    
      rc = select(STDIN_FILENO + 1, &f, NULL, NULL, &t);
      if (rc == -1)
      {
        /* select( ) error */
      }
      else if (rc == 0)
      {
        /* no input available */
      }
      else if (FD_ISSET(STDIN_FILENO, &f))
      {
        rc = read(STDIN_FILENO, buf, 127);
        if (rc == -1)
        {
          /* read( ) error */
        }
        else
        {
          buf[rc] = 0;
          printf("%s", buf);
        }
      }
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. C++ code in 1999
    By chen1279 in forum C++ Programming
    Replies: 6
    Last Post: 10-04-2006, 10:02 AM
  3. MFC include BS
    By VirtualAce in forum Windows Programming
    Replies: 4
    Last Post: 10-31-2005, 12:44 PM
  4. Function reference problems
    By Inquirer in forum C++ Programming
    Replies: 2
    Last Post: 05-07-2003, 09:03 AM
  5. help with finding lowest number entered
    By volk in forum C++ Programming
    Replies: 12
    Last Post: 03-22-2003, 01:21 PM