Speeding Up Internet Browsing


Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame

This article is based on another article I did, DNSWATCH, at http://www.codeguru.com. You might want to take a look at that before proceeding because this is a newer version with more features, and some new code. The program is based on DNS host name lookups done by programs, mainly browsers. It lists them and allows you to select some and save or append to a temporary file, and optionally update the host file. This allows you to block sites that are just advertisements, or tracking sites and eliminate the overhead of retrieving them. I basically wrote this program to try and watch who was watching me, and block annoying ads. It's also interesting just running it and see how many Web sites are looked up from one Web page.

Editing a system file (hosts) can be dangerous; however, most Windows users don't ever use it, so it's safe doing this. You can block yourself from some Web sites if you select the wrong URL. For example, if you selected "www.ebay.com," saved it, and updated the hosts file, you would not be able to access ebay at all. I added the "edit hosts file" option so you can remove any entries you want to.

I added some command line switches so I could start the program from startup. They are: "-s" to auto start the program, -b to turn off the beeping, and "-t tempfilename" to set the default temp file. After you add the program to the start menu, right-click it and select Properties. In the window will be a line marked Target and an EditBox next to it containing the full path and executable name. Just add the switches after the name. This is the one I use: C:\dev\NewDev\DNSWatch\Debug\DNSWatch.exe -s -b.

I also found a piece of code I have been looking for a long time. ListBoxes always scroll down and new entries can't be seen; the first entries are always seen. There may be other ways to do this, but by adding one line after adding an entry to the listbox it seems to work the opposite, newest at the bottom.



You now can edit the temporary file. I also added some code that invokes the CFileDialog dialog so you can search for a temporary file. I added an On-Top button because that's what I needed once. You now can edit the hosts file. This is not recommended, but it's necessary sometimes (like just checking). You now can update the hosts file with the entries in the temporary file automatically. In WinXP and Win2K, the hosts file resides in \windows\system32\drivers\etc directory. The logic I used for this task is:

  1. Copy the hosts file to a file named hosts.bak and create it or replace it in the etc directory.
  2. Open the temporary file for append. Read in the hosts file and append it to existing entries.
  3. Sort the new file, remove duplicates, and write the results to the hosts file.
  4. Empty the temporary file.

This works and I haven't had any problems yet. Additional backups could be done and the logic could be changed to taste. Here is most of the code I've added. It reads better in the compiler.

Note: You may have to delete and re-add ws2_lib.dll and iphlpapi.lib with the path of your SDK lib directory.
int CompHostNames(const void *s1, const void *s2)
return strcmp((const char *) s1, (const char *) s2);
int MergeSortAndDeleteDups()
FILE *fh;
int count=0;
char *host_array = (char *)calloc(1000*256,1);    //1000 entries
char ctemp[256], *cptr;

//read in hosts file
fh = fopen (hosts_file_name,"r");
if ( fh == NULL ) return 1;    //No default param file
while(true) {
cptr = fgets((char *)&ctemp,256,fh);
if (cptr == NULL ) break;
if (count >=1000) break;

//Now read and append the temporary file
fh = fopen (temp_file_name,"r");
if ( fh == NULL ) return 2;    //No default param file
while(true) {
cptr = fgets((char *)&ctemp,256,fh);
if (cptr == NULL ) break;
if (count >=1000) break;

//We have all the hosts, now sort the array

//Now remove duplicates and save in temp file
fh = fopen (temp_file_name,"w");
if ( fh == NULL ) {
return 3;    //???
char last_host[256] = "";
for(int i=0; i< count; i++){
strcpy(ctemp, &host_array[i*256]);
if (strcmp(last_host,ctemp) != 0) {
free(host_array);    //no longer needed

//Now copy temp to hosts
FILE *fhr = fopen (temp_file_name,"r");
if ( fhr == NULL ) {
return 4; //???

FILE *fhw = fopen (hosts_file_name,"w+");
if ( fh == NULL ) {
return 5;    //???
while(true) {
cptr = fgets((char *)&ctemp,256,fhr);
if (cptr == NULL ) break;
return 0;
void CDNSWatchDlg::OnUpdateButtonButton()
//Save a copy of current host file
int rc = CopyFile(hosts_file_name, hosts_file_name+".bak", FALSE);
temp_file_name = m_save_filename;
//Merge temp and host files, sort,remove duplicates, and update
//host file
rc = MergeSortAndDeleteDups();
if ( rc == 0) {    ///empty temp file
FILE *fh;
fh = fopen (temp_file_name,"w");
if ( fh == NULL ) return;    //??
m_status = "Hosts File Updated";
// I simply exec notepad from SystemRoot and edit hosts file
void CDNSWatchDlg::OnEditHostsButton()
char ctemp[256], sysroot[256];
strcat(ctemp,"\\notepad ");

// I exec notepad from SystemRoot and edit temp file
void CDNSWatchDlg::OnEditTempFile()
char sysroot[256], tstr[256];;
CString Newstr,Srchstr,Srchaddstr="\\",Repstr="\\\\",ctemp;

//this function adds a '\' to each '\' in the temp file display
CString Chstr = ConvertAndInsert(Newstr,m_save_filename,

//check if temp file doesn't exist, create it
FILE *fhr = fopen (temp_file_name,"a+");    //check if exists,
                                            //create else
if ( fhr == NULL ) {
m_status = "Error opening temp file";    //Error ???

strcat(tstr,"\\notepad ");
//This function takes a string, searches for another string in it,
//and inserts a string after the string found. I couldn't seen to
//find an easier way for a CString
CString ConvertAndInsert(CString Newstr,CString Srchstr,
                         CString Srchaddstr,CString Repstr)
int ndx = 0;
Newstr = Srchstr;
while(true) {
ndx = Srchstr.Find(Srchaddstr, ndx);
if (ndx == -1 ) break;
Srchstr = Newstr;
return Newstr;
//This is a function to toggle whether the dialog is always on
//top or not
void CDNSWatchDlg::OnOntopbutton()
int rc;
HWND hWandle;

if ( OnTop == TRUE ) {
OnTop = FALSE;
m_on_top_button.SetWindowText("Not On Top");

else {
OnTop = TRUE;
m_on_top_button.SetWindowText("On Top");
rc = ::SetWindowPos ( ::GetActiveWindow(), hWandle ,
if ( rc == 1) return;
rc = GetLastError ();
//This function allows you to search for a temp file using system
void CDNSWatchDlg::OnSearchBUTTON()
static LPCTSTR kpszFileTypes = _T(" (*.*)||");

CFileDialog fd(TRUE, _T(".acs"), NULL, OFN_HIDEREADONLY,
               kpszFileTypes, this);
if (fd.DoModal() == IDCANCEL) return;    //nothing selected or
m_save_filename = fd.GetPathName();      //get new temp file name

Known Quirks

  • On multi-homed systems where the first interface listed isn't active or is disabled, a bind error will occur.
  • If no interface is enabled, it will default to the loop back address.
  • If you use a proxy server for your browser, the hosts file is bypassed and no DNS lookups will show.


Date Posted: 09-28-2002
Modified 02-13-2003



  • Excellent technical article !!!!

    Posted by Legacy on 05/27/2003 07:00am

    Originally posted by: USA

    We learn a lot from your article, your solution is the best solution in the world.



  • WARNING: Your Code Can Crash Computers!

    Posted by Legacy on 05/23/2003 07:00am

    Originally posted by: Bill SerGio

    You fail to address several significant issues related to the hosts file that makes your suggested not practical.

    First, this idea is not new in terms of using the hosts file to block ads and it goes back as far as 1991 and probably earlier.

    Second, there is a physical limite to the number of entries that can be posted to the hosts file that makes your approach useless. In a simple test it is easy to get a list of over 13,000 ad sites in common usage and which I posted in a previous article posted here on your approach.

    If you put 13,000 which is really the MINIMUM number of sites needed to get any significant blocking of ads many computers will completely LOCK UP and require a reboot of the system and will STOP working completelty! YOU FAIL TO POINT THIS OUT.

    Why does this happen? It is due tothe fact there several of those annoying spyware programs open the hosts file and LOCK IT to prevent ad blockage dude!

    While abstarct approaches likes yours are interesting, your approach fails to work in any practical manner because of the reality of the Internet the the way the hosts file is used by many programs. Anything over a few hundred lines will CRASH most computers and you should WARN people about this!

    Again, while you idea to use the hosts file is not original your Winsock code is excellent but unfortunately your appraoch fails to achieve its goal because of the limitation on the size of the hosts file.

    Like I said, it would take AT LEAST 13,000 entries in the hosts file to achieve any useful ad blocking results!

  • Good Program

    Posted by Legacy on 02/24/2003 08:00am

    Originally posted by: Frank Jenson

    I used you program and got a good list of hosts NOT to go to. Surfing is definately faster. Here is the list: banner.linksynergy.com ad.doubleclick.net doubleclick.net m.doubleclick.net tribalfusion.com netidentity.com pro-market.net ke2.informative.com www.aadcom.com ehg-pennwell.hitbox.com trafficmp.com pro-market.net www.netster.com auto.search.msn.com statse.webtrendslive.com spylog.com fastclick.net www.cyblime.com capitalone.com lhg1.hitbox.com ru4.com servedby.advertising.com advertising.com switchboard.com toolbar.netscape.com ar.atwola.com www.qksrv.net cl.zedo.net gator.com www.nwsource.com adcoltroller.unicast.com adsl.intelliads.com barconet.com www.moreover.com ad.trafficmp.com ad.trafficmp.com adremote.pathfinder.com localhost stats.surfaid.ihost.com track.directleads.com oz.valueclick.com ads.adorigin.com ads.clickagents.com st.clickagents.com ie.search.msn.com ads.adorigin.com banners.pennyweb.com ads.addynamix.com focusin.ads.targetnet.com toolbar.aol.com stats.surfaid.ihost.com adfarm.mediaplex.com mediaplex.com www.looksmart.com zedo.net 11.zedo.net media.fastclick.net ads.x10.com www.netster.com endingtree.com srd.yahoo.com babel.altavista.com us.adserver.yahoo.com tscnads2.telescan.com view.atdmt.com spd.atdmt.com ads-direct.prodigy.net adserver.prodigy.net pclick.yahoo.com myclicks.prodigy.net chkpt.zdnet.com servedfor.valuead.com banners.valuead.com z1.adserver.com ads.ad-flow.com citi.bridgetrack.com mds.centrport.net wb.coolsavings.com c1.zedo.com l5.zedo.com c4.zedo.com ad.linksynergy.com ads.web.aol.com sc.musicmatch.com sc.musicmatch.com windowsmedia.com rd.intl.doubleclick.net ad.uk.doubleclick.net adlog.com.com www.vibrantmedia.com promo.search.com.com ads.com.com tribalfusion.speedera.net ads.specificpop.com static.smni.com ad.smni.com

  • Still not working on Windows XP

    Posted by Legacy on 02/18/2003 08:00am

    Originally posted by: Jase Jennings

    Despite the recent code update, this still does not work on Windows XP (at least not on mine anyway) ...

    Error :

    Ioctl() failed, errno=10022

    Which is a shame, because i found the article interesting and was looking forward to getting my hands dirty with this. Is it fixable ?


  • Speed Issue

    Posted by Legacy on 01/14/2003 08:00am

    Originally posted by: Sirsha

    How to increase the speed of modem booster

  • Give an example of a filter--do you mean this?

    Posted by Legacy on 10/16/2002 07:00am

    Originally posted by: Bill SerGio

    // Your Filter goes here. Don't do too much processing,
    // or you may drop packets

    If you are adding these domains to the local hosts file, WHY would you add any filter here? Please give an example of a filter you might add??????


  • Easy fix in most cases

    Posted by Legacy on 10/08/2002 07:00am

    Originally posted by: Pete

    There are a few ad sites that make up the majority. A real easy way to address this problem is to look at the source for the web pages. Figure out where they're getting the ads from, such as ads.doubleclick.net, various akamai.net servers, etc.

    Next, open up the hosts file (on my system it's in c:\winnt\system32\drivers\etc\). Ad each of these DNS entries and map it to

    Goodbye ads, hello speed. I have maybe 12 entries and it's made a huge difference. Cool program, though.

  • Good.

    Posted by Legacy on 10/07/2002 07:00am

    Originally posted by: Kandukondein

    Very informative.

  • drop packets

    Posted by Legacy on 10/05/2002 07:00am

    Originally posted by: Nathan

    How do i know if packets are being dropped?

  • value of SIO_RCVALL

    Posted by Legacy on 10/04/2002 07:00am

    Originally posted by: Bill Nolde

    I discovered that the value 0x98000001 works in the ioctl call.

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date