--------------------------- Using the readall_poor() function in the chat server --------------------------- -Ian! D. Allen - idallen@idallen.ca The stateful readall() can't be used in the chat server (selectserver.c); because, that server never forks and all the readall()s run in the same address space. All the client inputs would mix together in the single internal buffer. The single readall() in your client will work fine; it's only the selectserver that has the problem (and only with multiple clients). The kludge fix is to have a stateless readall() named readall_poor() that you use *only to build your selectserver*. Replace the calls to "read" with calls to "readall_poor" in your selectserver.c file. Don't use the readall() code in the selectserver; use readall_poor(). Here's the most of the code: // This function is used as a drop-in replacement for read() on a // socket. It reads enough bytes to find either a line ending in a // newline or a full buffer. The function has no internal state and // can be used on other descriptors at the same time. It is extremely // inefficient, since it calls read() for every character! It also // throws away any partial line collected when EOF/error happens. // // This function should only be used in the chat server "selectserver". // // Author: Ian D. Allen idallen@idallen.ca ssize_t readall_poor(int fd, char *buf, size_t len){ char *bufp = buf; // pointer to output buffer buf char *end = bufp+len; // end of output buffer buf ssize_t total = 0; // count of characters copied to buf for(;;) { if( bufp == end ) break; // output buffer is full; return int ret = read(fd,bufp,1); // read a single char if( ret <= 0 ){ if( ret < 0 ) perror("readall reading"); // BAD: this throws away a partially collected line at EOF/error total = 0; // return EOF indication to caller break; } bufp += ret; // move to next output position total += ret; // count the character written if( bufp[-1] == NEWLINE ) break; // return if last char written was \n } assert(total <= len); // "man 3 assert" assert(bufp <= end); return total; }