From owner-freebsd-questions Tue Oct 8 17:10:25 2002 Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 5C2BC37B401 for ; Tue, 8 Oct 2002 17:10:23 -0700 (PDT) Received: from rhymer.cogsci.ed.ac.uk (rhymer.cogsci.ed.ac.uk [129.215.144.8]) by mx1.FreeBSD.org (Postfix) with ESMTP id 3921B43E4A for ; Tue, 8 Oct 2002 17:10:22 -0700 (PDT) (envelope-from richard@cogsci.ed.ac.uk) Received: from sorley.cogsci.ed.ac.uk (sorley [129.215.144.53]) by rhymer.cogsci.ed.ac.uk (8.9.3/8.9.3) with ESMTP id BAA10678; Wed, 9 Oct 2002 01:10:20 +0100 (BST) Received: (from richard@localhost) by sorley.cogsci.ed.ac.uk (8.9.3+Sun/8.9.3) id BAA23132; Wed, 9 Oct 2002 01:10:20 +0100 (BST) Date: Wed, 9 Oct 2002 01:10:20 +0100 (BST) Message-Id: <200210090010.BAA23132@sorley.cogsci.ed.ac.uk> From: Richard Tobin Subject: Re: How i can force a stream socket to wait as limited time inaccept() function? To: Fernando Gleiser , alireza mahini In-Reply-To: Fernando Gleiser's message of Tue, 8 Oct 2002 17:59:44 -0300 (ART) Organization: just say no Cc: questions@FreeBSD.ORG Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG > 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 #include #include #include #include #include #include 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