DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

CPP Web Crawler

04.11.2006
| 9295 views |
  • submit to reddit
        Another program I found in my old source code directory.  This looks like another web crawler designed to run in Windows.

// http.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream.h>
#include <winsock2.h>
#include <stdio.h>


#define MAX_LENGTH 4096


void main()
      {
          int s_len;
          int skt_Smtp;
          int success;
		  int i;
          struct sockaddr_in st_Sockaddr;
		  char                 recv_Buf[MAX_LENGTH];
          char send_Buf[MAX_LENGTH];

          //Initialize Sockets
          WSADATA wsa;
          WSAStartup(MAKEWORD(2, 0), &wsa);


          skt_Smtp = socket(AF_INET,SOCK_STREAM,0);
          if (skt_Smtp < 0)
          {
              cout<< "Error Creating Socket"<<endl;
              return;
          }
         else
         {
              st_Sockaddr.sin_family = AF_INET;
              st_Sockaddr.sin_port = htons(80);

             //Get the IP address and initialize the structure
             st_Sockaddr.sin_addr.s_addr = inet_addr("143.53.238.9");

             success = connect(skt_Smtp,(struct sockaddr *) &st_Sockaddr,sizeof(st_Sockaddr));

             //do stuff
             strcpy(send_Buf,"GET /internal/index.php HTTP/1.1\r\nHost: www.brad.ac.uk\r\n\r\n");

             s_len = send(skt_Smtp,send_Buf,strlen(send_Buf),0);
	do
	{
		i = recv(skt_Smtp, recv_Buf, MAX_LENGTH, 0);
		if(i < 0)
		{
			printf("recv() returned %d, errno=%d\n", i, errno);
			break;
		}
		if(i > MAX_LENGTH)
			i = MAX_LENGTH;
		printf("%-*.*s", i, i, recv_Buf);
	}
	while(i != 0);


         WSACleanup();
}
}