According to POSIX, errno is set only if 'recv()' returns -1.
When connection has been closed, 'recv()' returns 0 and errno left untouched.
For functions which check errno value after 'swrite()', errno now is set to ECONNRESET,
so they produce correct message 'Connection reset by peer'.
if (recv(fd, buffer, sizeof(buffer), MSG_PEEK | MSG_DONTWAIT) == 0) {
/* if recv returns zero (even though poll() said there is data to be
* read), that means the connection has been closed */
- return errno ? errno : -1;
+ errno = ECONNRESET;
+ return -1;
}
}