Figure 1228 shows the main routine for the echo

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: riten(int fd, void *buf, size t count); both return: number of bytes read (0 if EOF) or written, -1 on error The code for these functions is shown in Figure 12.15. The readn function returns a short count only when the input operation extends past the end of file. Other short counts are handled by repeatedly invoking read until count bytes have been transferred. The writen function never returns a short count. 622 CHAPTER 12. NETWORK PROGRAMMING code/src/csapp.c 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ssize_t readn(int fd, void *buf, size_t count) { size_t nleft = count; ssize_t nread; char *ptr = buf; while (nleft > 0) { if ((nread = read(fd, ptr, nleft)) < 0) { if (errno == EINTR) nread = 0; /* and call read() again */ else return -1; /* errno set by read() */ } else if (nread == 0) break; /* EOF */ nleft -= nread; ptr += nread; } return (count - nleft); /* return >= 0 */ } code/src/csapp.c code/src/csapp.c 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ssize_t writen(int fd, const void *buf, size_t count) { size_t nleft = count; ssize_t nwritten; const char *ptr = buf; while (nleft > 0) { if ((nwritten = write(fd, ptr, nleft)) <= 0) { if (errno == EINTR)...
View Full Document

This note was uploaded on 09/02/2010 for the course ELECTRICAL 360 taught by Professor Schultz during the Spring '10 term at BYU.

Ask a homework question - tutors are online