Article View: comp.unix.programmer
Article #4Problem making accept() non-blocking (never sees connections)
From: Ben Mehlman
Date: Tue, 27 Oct 1992 19:16
Date: Tue, 27 Oct 1992 19:16
70 lines
2368 bytes
2368 bytes
Hello!
I'm having a problem trying to use non-blocking I/O on a
(stream accepting) socket.
The manuals and man pages say that if you use fcntl() to
set a stream socket to non-blocking, accept() calls on that
socket will return -1 with an errno of EWOULDBLOCK IF
there are no connections pending.
My experience is: When the FNDELAY flag on the socket is not
set, the connections are established w/ no problem, but
accept() will block waiting for connections (as expected).
If FNDELAY is set, then accept will always return
EWOULDBLOCK, even when there are pending connects.
Here's the code (minus error checking etc..):
struct sockaddr_in sin = {AF_INET};
struct sockaddr_in from;
int sock, size;
int port = 2000;
/* create socket */
if((srvr->sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
/* set for non-blocking i/o, preserving other flags */
if(fcntl(srvr->sock, F_SETFL, fcntl(srvr->sock, F_GETFL, 0)
|FNDELAY) < 0)
sin.sin_port = port;
/* bind socket to port */
if(bind(srvr->sock, (struct sockaddr *)&sin, sizeof(sin)) < 0)
/* post listen */
listen(srvr->sock, 5);
int sock, size, err = 0;
struct sockaddr_in from;
size = sizeof(from);
sock = accept(srvr->sock, (struct sockaddr *)&from, &size);
switch(errno)
{
case 0:
break;
case EWOULDBLOCK:
return(1);
break;
default:
perror("RService: accept");
return(0);
break;
}
Like I said, if I remove the fcntl() it works fine, with blocking.
If I set FNDELAY, I always get EWOULDBLOCK from accept().
Thanks for any assistance you can provide...
--
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
\|/ Ben Mehlman Indigo..
^ ^ Trirex Systems Inc
# # bmehlman@trirex.com Indigoing..
@
\___/ >NeXTMail Welcome< Indigone.
Technical Details
Message-ID:
<1992Oct27.191608.11099@Trirex.COM>Path:
arc1-ca.pugleaf.net!sparky!uunet!newshost!rootShow Full Headers
Complete Article Headers
Newsgroups: comp.unix.programmer Path: arc1-ca.pugleaf.net!sparky!uunet!newshost!root From: bmehlman@trirex.com(Ben Mehlman) Subject: Problem making accept() non-blocking (never sees connections) Message-ID: <1992Oct27.191608.11099@Trirex.COM> Keywords: sockets, accept, fcntl Sender: root@Trirex.COM (Operator) Reply-To: bmehlman@trirex.com Organization: Trirex Systems Inc. Date: Tue, 27 Oct 1992 19:16:08 GMT Lines: 70