Date: Wed, 9 Oct 2002 01:10:20 +0100 (BST) From: Richard Tobin <richard@cogsci.ed.ac.uk> To: Fernando Gleiser <fgleiser@cactus.fi.uba.ar>, alireza mahini <alirezamahini@yahoo.com> Cc: questions@FreeBSD.ORG Subject: Re: How i can force a stream socket to wait as limited time inaccept() function? Message-ID: <200210090010.BAA23132@sorley.cogsci.ed.ac.uk> In-Reply-To: Fernando Gleiser's message of Tue, 8 Oct 2002 17:59:44 -0300 (ART)
next in thread | raw e-mail | index | archive | help
> 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 majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200210090010.BAA23132>