Информация об изменениях

Сообщение Re[3]: 10K problem for keep-alive utility от 11.11.2023 9:39

Изменено 11.11.2023 11:25 avovana

Re[3]: 10K problem for keep-alive utility
Здравствуйте, landerhigh, Вы писали:

L>И вот после всего этого будет очень забавно обнаружить, что необходимо также отслеживать half-open TCP соединения.


Вот да. Тоже мысль пришла, что epoll_wait с событием на чтение + последующий read смогут показать, что соединение закрыто штатно:
n = read(socketfd, buffer, ...)
if (n == 0) // peer disconnected
    break;
else if (n == -1) // error
{
    perror("read");
    break;
}
else // received 'n' bytes
{
    printf("%.*s", n, buffer);
}


А не штатно? Сеть пропала, сервер взорвался. Нашёл такое на so:

epoll_wait will return a EPOLLHUP or EPOLLERR for the socket if the other side disconnects. EPOLLHUP and EPOLLERR are set automatically but you can also set the newer EPOLLRDHUP which explicitly reports peer shutdown.
Also if you use send with the flag MSG_NOSIGNAL it will set EPIPE on closed connections.
int resp = send ( sock, buf, buflen, MSG_NOSIGNAL );

if ( resp == -1 && errno == EPIPE ) { /* other side gone away */ }

Much nicer than getting a signal.

Есть ответы про фичу сокета KEEP-ALIVE. Но там нет единого мнения насколько это нормально работает.
Re[3]: 10K problem for keep-alive utility
Здравствуйте, landerhigh, Вы писали:

L>И вот после всего этого будет очень забавно обнаружить, что необходимо также отслеживать half-open TCP соединения.


Вот да. Тоже мысль пришла, что epoll_wait с событием на чтение + последующий read смогут показать, что соединение закрыто штатно:
n = read(socketfd, buffer, ...)
if (n == 0) // peer disconnected
    break;
else if (n == -1) // error
{
    perror("read");
    break;
}
else // received 'n' bytes
{
    printf("%.*s", n, buffer);
}


А не штатно? Сеть пропала, сервер взорвался. Нашёл такое на so:

epoll_wait will return a EPOLLHUP or EPOLLERR for the socket if the other side disconnects. EPOLLHUP and EPOLLERR are set automatically but you can also set the newer EPOLLRDHUP which explicitly reports peer shutdown.
Also if you use send with the flag MSG_NOSIGNAL it will set EPIPE on closed connections.
int resp = send ( sock, buf, buflen, MSG_NOSIGNAL );

if ( resp == -1 && errno == EPIPE ) { /* other side gone away */ }

Much nicer than getting a signal.

Есть ответы про фичу сокета KEEP-ALIVE. Но там нет единого мнения насколько это нормально работает.

upd. Ещё что-то с MSG_PEAK придумывают. Когда можно посмотреть если ли что-то во внутреннем буфере, не выгребая из него.