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