套接字与c标准io

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(),会造成严重的问题(至于是什么样子的严重上面的链接有讲,这里就不在赘述)。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值