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

C++ Socket library tutorial

Socks4 client functionality

Prev   |   Menu   |   Next

This program shows how to enable the socks4 client functionality. Set socks4 server parameters with the SocketHandler class. All sockets using that socket handler will then use that socks4 server for outgoing connections.

httpget2_socks4.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;

	// enable socks4 client
	h.SetSocks4Host("somehost.com");
	h.SetSocks4Port(1080);
	h.SetSocks4Userid("myname.net");
	h.SetSocks4TryDirect(false);

	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