Цитата(Сергей Борщ @ Sep 14 2017, 13:01)

Все уже придумано за нас. Почитайте про SO_KEEPALIVE. Там по умолчанию довольно большие таймауты, но вы можете установить свои.
нашел такой пример
Код
/* Set the option active */
optval = 1;
optlen = sizeof(optval);
if(setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen) < 0) {
perror("setsockopt()");
close(s);
exit(EXIT_FAILURE);
}
printf("SO_KEEPALIVE set on socket\n");
это чтоб включить SO_KEEPALIVE. компилируется без ошибок.
а как теперь проверить жив сокет или нет?
я так понимаю с выставленной опцией где то бежит проверка каждые tcp_keepalive_intvl и наверно выставляется флаг гдето в статусном регистре, который надо проверять.
Цитата
The SO_KEEPALIVE option causes a packet (called a 'keepalive probe')
to be sent to the remote system if a long time (by default, more than
2 hours) passes with no other data being sent or received. This packet
is designed to provoke an ACK response from the peer. This enables
detection of a peer which has become unreachable (e.g. powered off or
disconnected from the net).
а где я вижу этот ACK response from the peer?
Цитата(Эдди @ Sep 14 2017, 12:11)

Все надо проверять.
Здесь я намучился с этими чертовыми сокетами! Сокет - не файл, запись в закрытый с другой стороны сокет никаких ошибок не даст! Поэтому нужно вводить таймауты и/или периодические рукопожатия.
у меня этот код компилируется.
Код
static int waittoread(int sock){
fd_set fds, efds;
struct timeval timeout;
int rc;
timeout.tv_sec = 1; // wait not more than 1 second
timeout.tv_usec = 0;
FD_ZERO(&fds);
FD_ZERO(&efds);
FD_SET(sock, &fds);
FD_SET(sock, &efds);
do{
rc = select(sock+1, &fds, NULL, &efds, &timeout);
if(rc < 0){
if(errno != EINTR){
putlog("Server not available");
WARN("select()");
return -1;
}
continue;
}
break;
}while(1);
if(FD_ISSET(sock, &efds)) return -1; // exception - socket closed
if(FD_ISSET(sock, &fds)) return 1;
return 0;
}
это значит так можно проверить и сервер и сокет?
Сообщение отредактировал Jenya7 - Sep 14 2017, 09:10