码迷,mamicode.com
首页 > 系统相关 > 详细

linux网络编程中需要注意的信号SIGPIPE

时间:2017-07-01 19:30:49      阅读:276      评论:0      收藏:0      [点我收藏+]

标签:log   div   har   connect   closed   ogr   lock   提示   cte   

  在调试cs时,s端循环收,c端循环发,s端意外崩溃后,c端自动退出,终端提示SIGPIPE导致c端退出。man 7 signal:

SIGPIPE      13      Term    Broken pipe: write to pipe with no readers

  SIGPIPE发生在:写一个没有读的管道,默认动作是终止程序。man 2 write:

EPIPE  fd is connected to a pipe or socket whose reading end is closed.
              When this happens the writing process will also receive  a  SIG-
              PIPE  signal.  (Thus, the write return value is seen only if the
              program catches, blocks or ignores this signal.)

  write的错误码EPIPE发生在写的fd对应的pipe或者socket对端的读端关闭了时。写操作的返回值仅当程序捕获,阻塞,忽略此信号时才能收到,因为SIGPIPE默认会终止程序。

  有一个细节,使用gdb单步调试c端,在开始write前,手动终止s端,c端收到s的FIN分节,并回复ack,c端处于CLOSE_WAIT状态,此时,c端的write操作返回的数值是正常的,socket状态不存在了,wireshark抓包会发现一个RST包,表示socket连接已重置,再次write,将会立即收到SIGPIPE。准确的讲写一个已经收到RST分节的socket会产生SIGPIPE。

linux网络编程中需要注意的信号SIGPIPE

标签:log   div   har   connect   closed   ogr   lock   提示   cte   

原文地址:http://www.cnblogs.com/thammer/p/7103065.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!