/* readn.c - From Jon C. Snader: Effective TCP/IP Programming * Addison-Wesley 2000 * For better understanding, read the excellent Snader book. */ #include "etcp.h" /* readn - read exactly len bytes from socket fd into buffer bp */ /* It returns the number of characters actually read */ /* if successful, -1 in case of error. */ /* The buffer is assumed to be of size at least len, and*/ /* the data read is left as is, without '\0'. */ int readn( SOCKET fd, char *bp, size_t len) { int cnt; int rc; cnt = len; while ( cnt > 0 ) { rc = recv( fd, bp, cnt, 0 ); if ( rc < 0 ) /* read error? */ { if ( errno == EINTR ) /* interrupted? */ continue; /* restart the read */ return -1; /* return error */ } if ( rc == 0 ) /* EOF? */ return len - cnt; /* return short count */ bp += rc; cnt -= rc; } return len; }