简体   繁体   中英

how to inform tcp client of server shutdown

Using non-blocking (20 ms cycle) TCP connection in linux, I got a problem:

  • when I close socket from the server side [close(sd) or shutdown(sd,2);close(sd)], the client poll() receives no POLLHUP event.

  • when server is killed from shell, POLLHUP is received.

How can I inform client in a cycle or two?

A TCP disconnect is signalled with POLLIN, and a read() will return 0 in the case of a graceful shutdown, or -1 and an appropriate error (errno being anything but EINTR/EWOULDBLOCK).

There are platforms where it might be signaled with POLLHUP, so you might want to handle that case too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM