鍍金池/ 問答/Java  C  C++  Linux  HTML/ read一個(gè)收到RST的socket會(huì)返回0?

read一個(gè)收到RST的socket會(huì)返回0?

研究muduo網(wǎng)絡(luò)庫(kù),發(fā)現(xiàn)它在收到RST的包時(shí)觸發(fā)可讀事件,然后讀的時(shí)候返回了0,然后關(guān)閉TCP連接。

但是讀一個(gè)收到RST的socket不是應(yīng)該返回-1,并且設(shè)置errno為ECONNRESET

20180115 12:57:41.647071Z  7171 TRACE printActiveChannels {8: IN HUP ERR }  - EventLoop.cc:323
20180115 12:57:41.647075Z  7171 TRACE handleEventWithGuard 8: IN HUP ERR  - Channel.cc:86
20180115 12:57:41.647082Z  7171 ERROR TcpConnection::handleError [EchoServer-0.0.0.0:2007#1] - SO_ERROR = 104 Connection reset by peer - TcpConnection.cc:426
20180115 12:57:41.647093Z  7171 TRACE handleClose fd = 8 state = kConnected - TcpConnection.cc:411
20180115 12:57:41.647096Z  7171 TRACE updateChannel fd = 8 events = 0 index = 1 - EPollPoller.cc:113
20180115 12:57:41.647102Z  7171 TRACE update epoll_ctl op = DEL fd = 8 event = { 8:  } - EPollPoller.cc:181
20180115 12:57:41.647108Z  7171 INFO  EchoServer - 192.168.0.106:33334 -> 192.168.0.106:2007 is DOWN - echo.cc:27
20180115 12:57:41.647113Z  7171 INFO  TcpServer::removeConnectionInLoop [EchoServer] - connection EchoServer-0.0.0.0:2007#1 - TcpServer.cc:112
20180115 12:57:41.647122Z  7171 TRACE removeChannel fd = 8 - EPollPoller.cc:157
20180115 12:57:41.647142Z  7171 DEBUG ~TcpConnection TcpConnection::dtor[EchoServer-0.0.0.0:2007#1] at 0x1FE16E0 fd=8 state=kDisconnected - TcpConnection.cc:70

上面是Muduo打印出來的日志,可以看到確實(shí)是收到RST,但是為什么read會(huì)返回0了?看到很多資料都是read和write一個(gè)收到RST的socket都是返回-1,然后設(shè)置erno。

我的理解中只有收到FIN的socket,read才返回0,收到RST的socket會(huì)返回-1。

回答
編輯回答
撥弦

收到RST的socket,第一次觸發(fā)可讀,read返回-1并設(shè)置errno,不close得話,繼續(xù)觸發(fā)可讀,再read便會(huì)返回0。

2017年11月30日 15:02