I'm trying to detect when a connection has been dropped.
On this way, it only detects that the connection is no longer active when the internet is back, but not when it has been dropped actually.
int loop = 1;
long int msConn;
// Main thread
void connect() {
// ...
while (loop) {
if ((sizeBytes = recv(sockfd, buffer, MAX_BUFFER_SIZE - 1, 0)) == -1) {
cout << "Error: connection lost." << endl;
sleep(15);
connect();
return;
}
// Update timestamp
struct timeval tp;
gettimeofday(&tp, NULL);
msConn = tp.tv_sec * 1000 + tp.tv_usec / 1000;
}
}
// Heartbeat thread
void checkConnection() {
for(;;) {
struct timeval tp;
gettimeofday(&tp, NULL);
long int ms = tp.tv_sec * 1000 + tp.tv_usec / 1000;
if (msConn != 0) {
if ((ms - msConn) >= 10000) {
msConn = 0;
close(sockfd);
}
}
sleep(15);
}
}
Aucun commentaire:
Enregistrer un commentaire