Questa è una vecchia versione del documento!
Autore: Fabio Di Matteo
Ultima revisione: 09/01/2021 - 00:21
#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #include <stdlib.h> int serverIsUp(const char* host, int port) { int sd; struct sockaddr_in client; struct hostent *hp; hp = gethostbyname(host); bzero(&client, sizeof(client)); client.sin_family = AF_INET; client.sin_port = htons(port); client.sin_addr.s_addr =((struct in_addr*)(hp->h_addr))->s_addr; if((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) printf("Errore nella creazione del socket!\n"); else printf("socket creato!\n"); if (connect(sd, (struct sockaddr *)&client, sizeof(client)) == 0){ printf("Server is UP!\n"); close(sd); return 1 ; }else{ printf("Server is DOWN!\n"); close(sd); return 0; } }
Il nostro wiki installa solamente cookie tecnici necessari al funzionamento della piattaforma "Dokuwiki". Niente analitics, statistiche, tracciamenti o altro.