How to know connection disconnected while receive paused

Marc Lehmann schmorp at
Fri Jun 21 21:29:06 CEST 2019

On Fri, Jun 21, 2019 at 06:51:03PM +0000, Tan Xiaofan <xfan1024 at> wrote:
>     I am learning how to write a server program using libev, but I encounter some confusion.
>     I write a very simple server, the server receive data and send back intact. In order to handle the case of receive speed fast than send speed,
>     I pause data receive when send back not complete, send remain data while EV_WRITE event coming, restart receive when remain data send finished.

In general, with e.g. TCP sockets, you are the only one who can close the
connection, and only your side. And you can't detect a connection close by
the other end without reading all data.

For reading, that means you you need to call read/recv/etc., and on EOF
(remote connection close) you get a zero length read.

For write, you will get an error, but usually you will have to initiate
the shutdown from your side anyway, and handle errora on both read/write as

If you don't read, you can't detect a remote connection close, and in
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.

>     When receive paused, peer close socket after I call send(), I can not receive any event to indicate the connection is disconnected.
>     How to fix it?

Well, if thew other side closed the socket, then eventually you will get an
error on send as well (most likely ECONNRESET).

So, you just write yozur server normally, you read whenever oyu want, you
write whenever you want, you use libev whenever you want to be notified of
being able to read or write, and you handle errors as normal.

There is nothing else you need to do, and nothing else you can do, and
probably nothing else you should do :)

                The choice of a       Deliantra, the free code+content MORPG
      -----==-     _GNU_    
      ----==-- _       generation
      ---==---(_)__  __ ____  __      Marc Lehmann
      --==---/ / _ \/ // /\ \/ /      schmorp at
      -=====/_/_//_/\_,_/ /_/\_\

More information about the libev mailing list