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) {
		return -1;
	if (connect(sock, serv->ai_addr, serv->ai_addrlen) == -1) {
		printf("%s:%s is DOWN\n", host, proto);
	else {
		printf("%s:%s is UP\n", host, proto);
	return 0;

Kind Regards,
Nathaniel Blackburn

Edited by shadowscape: n/a

5 Years
Discussion Span
Last Post by JasonHippy

Looking at the code you've posted (which is C rather than C++), it seems to be using a simple sockets connection to determine whether the website is up or down. The headers included in the listing that you don't have, or don't recognise are all Linux/Unix specific headers.

I'm still familiarising myself with Linux programming so I may be wrong, but I don't think anything from "sys/types.h" and "arpa/inet.h" is being used in the listing. So these headers are not required and can probably be discounted/removed.

Regarding the other Linux specific headers:
1. "unistd.h" is used for the call to close(sock); at line 50. Not sure what to suggest there for Windows. (The winsock function closesocket()?)
2. "sys/socket.h" is required in order to be able to use sockets in Linux. For windows you'll almost certainly need to use "winsock2.h".
3. "netdb.h" is required for the definitions of the addrinfo struct and the getaddrinfo() and freeaddrinfo() functions. Again, these should be implemented or at least have equivalents somewhere in the Windows Sockets API.

From what I've seen, the code you've posted should be fairly easy to port to Windows. You just need to bone up on the Windows Sockets API. It's been ages since I did anything with sockets on Windows, but MSDN documentation can be found here:

And I assume you know what you're doing with regard to creating a .dll, so I'll leave that side of things alone for now!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.