How to know connection disconnected while receive paused

Marc Lehmann schmorp at schmorp.de
Sat Jun 22 00:56:13 CEST 2019


On Fri, Jun 21, 2019 at 08:25:22PM +0000, Tan Xiaofan <xfan1024 at live.com> wrote:
> > general, there is no way to do it, neither for you nor for libev - you
> > have to read the data that was sent to you until you get EOF or an error.
> 
> If user space receive buffer is full, program can't call recv() because there is no buffer to use.

Then you can't detect EOF, really.

> > Well, if thew other side closed the socket, then eventually you will get an error on send as well (most likely ECONNRESET).
> 
> I call send() before protocol stack receive FIN packet from client,

A FIN from the rmeote end does not close the connection. This means it is
perfectly valid to call send, and the data will be delivered to the remote
host.

All a FIN does is signal that the remote host stopped sending dats ("end
of file").

> It should have waited a EV_WRITE event and continue to send remain data, but in this time protocol stack received client FIN packet,
> so transmit will be stopped and EV_WRITE never to come again, I have no opportunity call send() again to get that ECONNRESET code.
> Is there some way to handle this case?

Libev, your kernel and not even TCP/IP can know that the remote end
closing the connection means you also want to close the connection. Thats
policy you have to implement yourself, and the way to do so is generally
to consume the data you received until you hit EOF.

The key to understand here is that the connection isn't closed at that
point, so it wouldn't help you if you could detect a closed conenction
(which probably could be done e.g. using getpeername). But even after the
remote end sends a FIN, the connection is still open.

It's just how TCP works.

-- 
                The choice of a       Deliantra, the free code+content MORPG
      -----==-     _GNU_              http://www.deliantra.net
      ----==-- _       generation
      ---==---(_)__  __ ____  __      Marc Lehmann
      --==---/ / _ \/ // /\ \/ /      schmorp at schmorp.de
      -=====/_/_//_/\_,_/ /_/\_\



More information about the libev mailing list