Hello,
I'm trying to write a function for a DLL in c++ what would check if a website is online or not and return either a error or the status code returned from the server, is there are any clever people out there, your help would be awesome, thanks.
i managed to get this code, but its for a application not a DLL, and seems to require some additional files i dont seem to have, could someone help improve upon this code, thanks.
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
int websitecheck(int argc, char **argv)
{
int status;
struct addrinfo hints;
struct addrinfo *serv;
int sock;
char *proto;
char *host;
if (argc != 3) {
printf("cara pakai:port_check host proto/port\n");
return -1;
}
host = argv[1];
proto = argv[2];
printf("host = %s, proto/port = %s\n", host, proto);
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC; //don't care IPV4 or IPV6
hints.ai_socktype = SOCK_STREAM; //TCP
hints.ai_flags = AI_PASSIVE; //fill in my IP for me
if((status = getaddrinfo(host, proto, &hints, &serv)) != 0) {
fprintf(stderr, "getaddrinfo error:%s\n",gai_strerror(status));
return -1;
}
sock = socket(serv->ai_family, serv->ai_socktype, serv->ai_protocol);
if (sock == -1) {
perror("socket");
return -1;
}
if (connect(sock, serv->ai_addr, serv->ai_addrlen) == -1) {
perror("connect");
printf("%s:%s is DOWN\n", host, proto);
}
else {
printf("%s:%s is UP\n", host, proto);
}
close(sock);
freeaddrinfo(serv);
return 0;
}
Kind Regards,
Nathaniel Blackburn