Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!seismo!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.unix.wizards Subject: Re: Non-blocking accept(2) calls Message-ID: <8395@mimsy.UUCP> Date: Thu, 3-Sep-87 22:38:37 EDT Article-I.D.: mimsy.8395 Posted: Thu Sep 3 22:38:37 1987 Date-Received: Sat, 5-Sep-87 13:54:45 EDT References: <479@radio.toronto.edu> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 46 Keywords: socket non-blocking BSD Summary: use listen followed by select-for-read In article <479@radio.toronto.edu> brian@radio.toronto.edu (Brian Glendenning) writes: >I am writing a graphics "server" on our Sun (3/180, SunOs 3.4=BSD 4.2), which >I am implementing through socket i/o. When the server is not connected I would >like to poll accept(2) periodically looking for new connections. It should be >non-blocking so that it doesn't adversely affect screen activity. > >So my question is - how do I make accept non-blocking? I know it can be done. Instead of making accept nonblocking, perform it only when it will not block. To find out when the latter is true, select the service socket (the one on which you have listen()ed) for reading: #include /* beware, 4.3BSD/V8 macros below */ fd_set rset, wset; ... if (listen(serverfd, 5)) error(...); FD_ZERO(&rset); FD_SET(serverfd, &rset); highfd = serverfd; for (;;) { fd_set r = rset, w = wset; int newfd, sourcesize; struct sockaddr_in source; nsel = select(highfd, &r, &w, (fd_set *)0, (struct timeval *)0); if (nsel <= 0) { /* if (errno == EINTR) continue; (optional) */ error(...); } if (FD_ISSET(serverfd, &r)) { sourcesize = sizeof(source); newfd = accept(servfd, &source, &sourcesize); if (newfd < 0) error(...); FD_SET(newfd, &rset); FD_SET(newfd, &wset); } ... } -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690) Domain: chris@mimsy.umd.edu Path: seismo!mimsy!chris