Code:
#pragma comment( lib, "ws2_32.lib" )
#include <stdio.h>
#include <Windows.h>
#include <WinSock.h>
#include <string.h>
#define MAX_BUF 32766
int main(int argc, char *argv[])
{
char httpRequest[MAX_BUF] = {0};
char httpParameter[] = "GET /index.php HTTP/1.0\r\nHost:";
char httpHost[] = "twitter.com";
char httpSub[] = "\r\n";
char httpResponse[MAX_BUF] = {0};
char httpBuffer[MAX_BUF] = {0};
char *httpAddr;
int httpLength = MAX_BUF;
int httpPort = 80;
struct sockaddr_in serveraddr;
SOCKET sock;
WSADATA wsaData;
PHOSTENT remoteHost;
strncat(httpRequest,httpParameter,strlen(httpParameter));
strncat(httpRequest,httpHost,strlen(httpHost));
strncat(httpRequest,httpSub,strlen(httpSub));
strncat(httpRequest,httpSub,strlen(httpSub));
if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0)
{
return 1;
}
if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
{
return 2;
}
remoteHost = gethostbyname(httpHost);
if(remoteHost == NULL)
{
return 3;
}
httpAddr = inet_ntoa (*(struct in_addr *)*remoteHost->h_addr_list);
memset(&serveraddr, 0, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = inet_addr((const char*)httpAddr);
serveraddr.sin_port = htons((unsigned short) httpPort);
if (connect(sock, (struct sockaddr *) &serveraddr, sizeof(serveraddr)) < 0)
{
return 4;
}
if (send(sock, httpRequest, strlen(httpRequest), 0) != strlen(httpRequest))
{
return 5;
}
do
{
httpLength = recv(sock, httpBuffer, MAX_BUF, 0);
if ( httpLength > 0 && (httpLength+strlen(httpResponse) <= MAX_BUF))
{
strncat(httpResponse, httpBuffer, httpLength);
}
else if( httpLength == 0 )
{
}
else
{
return 6;
}
}
while( httpLength > 0 );
printf("%s",httpResponse);
Sleep(20000);
return 0;
}