Home  +  Forums  +  C++ and Sockets  +  C++ and SQL: MySQL, sqlite, ODBC  +  Miscellaneous Projects
Logo
~Sockets~
~New~
~Examples~
~Contact~

C++ Socket library tutorial

Asynchronous DNS

Prev   |   Menu   |   Next

This is a small program that shows how to enable asynchronous dns. It's a slightly modified httpget2.cpp example. EnableResolver() starts a resolver server in its own thread.

httpget2_async_dns.cpp
#ifdef _WIN32
#define strcasecmp stricmp
#endif
#include <HttpGetSocket.h>
#include <HttpsGetSocket.h>
#include <SocketHandler.h>
#include <Parse.h>


void Get(SocketHandler& h,const std::string& url_in)
{
	Parse pa(url_in,":/");
	std::string protocol = pa.getword();
	std::string host = pa.getword();
	int port;
	{
		Parse pa(host,":");
		pa.getword();
		port = pa.getvalue();
	}
	std::string url = "/" + pa.getrest();
	std::string file;
	{
		Parse pa(url,"/");
		std::string tmp = pa.getword();
		while (tmp.size())
		{
			file = tmp;
			tmp = pa.getword();
		}
	}
	if (!strcasecmp(protocol.c_str(),"http"))
	{
		HttpGetSocket *s = new HttpGetSocket(h,host,port ? port : 80,url,file);
		s -> SetDeleteByHandler();
		h.Add(s);
	}
	else
	if (!strcasecmp(protocol.c_str(),"https"))
	{
		HttpsGetSocket *s = new HttpsGetSocket(h,host,port ? port : 443,url,file);
		s -> SetDeleteByHandler();
		h.Add(s);
	}
	else
	{
		printf("Unknown protocol: '%s'\n",protocol.c_str());
	}
}


int main(int argc,char *argv[])
{
	SocketHandler h;

	// add this line to enable asynchronous dns
	h.EnableResolver();

	for (int i = 1; i < argc; i++)
	{
		Get(h,argv[i]);
	}
	h.Select(1,0);
	while (h.GetCount())
	{
		h.Select(1,0);
	}
}



Prev   |   Menu   |   Next

Valid HTML 4.01!

Validate
Page, code, and content Copyright (C) 2021 by Anders Hedström