csapp 10.9节讲为什么不能在套接字上面使用标准io,而是费力气去包装unix io,以缓冲区的存在减少读写某个文件描述符时陷入内核的次数。
(1) The restrictions of standard I/O
Restriction 1: Input functions following output functions. An input function cannot follow an output function without an intervening call to fflush, fseek, fsetpos, or rewind. The fflush function empties the buffer associated with a stream. The latter three functions use the Unix I/O lseek function to reset the current file position.
Restriction 2: Output functions following input functions. An output function cannot follow an input function without an intervening call to fseek, fsetpos, or rewind, unless the input function encounters an end-of-file.
(2) It is illegal to use the lseek function on a socket.
有人提到过这个问题
https://stackoverflow.com/questions/20605967/restriction-of-c-standard-i-o-and-why-we-cant-use-c-standard-i-o-with-sockets
csapp中讲到,第一个限制可以使每次标准io输入操作都刷新一次缓冲区,但第二个限制只能对一个socket打开两个流,但这样会需要调用两次fclose(),会造成严重的问题(至于是什么样子的严重上面的链接有讲,这里就不在赘述)。