Date: 23 Nov 2001 00:58:08 -0500 From: Arcady Genkin <antipode@thpoon.com> To: freebsd-chat@freebsd.org Subject: Reentrant readn() and writen() for socket communication Message-ID: <873d363u6n.fsf@tea.thpoon.com>
next in thread | raw e-mail | index | archive | help
Stevens' UNIX Network Programming show sample readn()[1] and writen() functions to read/write from/to a socket. The functions handle interrupted read()/write() system calls by examining errno value. This approach is not thread-safe (because of usage of errno global). Is there a reentrant way to detect under-reads/writes, or had I better off with just using recv() with MSG_WAITALL flag? Footnotes: [1] Here's the code of readn() for easy reference: ssize_t readn( int fd, void *vptr, size_t n ) { size_t nleft; ssize_t nread; char *ptr; ptr = vptr; nleft = n; while ( nleft > 0 ) { if ( (nread = read(fd, ptr, nleft)) < 0 ) { if (errno == EINTR) nread = 0; /* and call read() again */ else return (-1); } else if ( nread == 0 ) break; /* EOF */ nleft -= nread; ptr += nread; } return (n - nleft); /* >= 0 */ } Many thanks, -- Arcady Genkin i=1; while 1, hilb(i); i=i+1; end To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-chat" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?873d363u6n.fsf>