> You need to set the socket descriptor in non-blocking mode, then call
> accept. accept will fail and return -1 with errno set to EWOULDBLOCK,
> the call select(2) on the socket. select will return when a connection
> arrives (you need to test for it) or when the timeout expires.

You don't need to put the socket in non-blocking mode to select for
accept.  See example program below.

-- Richard

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>


int main(int argc, char **argv)
{
    static struct sockaddr_in addr;
    int s;
    fd_set fds;
    struct timeval t = {5, 0};

    s = socket(PF_INET, SOCK_STREAM, 0);
    addr.sin_family = AF_INET;
    addr.sin_port = htons(atoi(argv[1]));
    if(bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0)
    {
        perror("bind");
        return 1;
    }
    listen(s, 5);
    FD_ZERO(&fds);
    FD_SET(s, &fds);
    switch(select(s+1, &fds, 0, 0, &t))
    {
      case 0:
        printf("timed out\n");
        return 0;
      case -1:
        perror("select");
        return 1;
      default:
        printf("select returned\n");
        printf("accept returned %d\n", accept(s, 0, 0));
        return 0;
    }
}

To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-questions" in the body of the message

Reply via email to