The Hypertext Transfer Protocol HTTP is the foundation proto

The Hypertext Transfer Protocol HTTP is the foundation protocol of the World Wide Web and can be used in any client/server application involving hypertext. HTTP is a protocol for transferring plaintext, hypertext, audio, images, or any Internet-accessible information^1. HTTP is an application layer protocol and is typically used between a Web browser and a Web server. A Web server is a machine (or a software) that stores websites contents. The aim of this project is to give students more insights, from a programming perspective, on how the interaction between the Web browser (Chrome, Internet Explorer, Firefox ... etc) and the Web server happens. Use your knowledge in sockets programming to write the following programs: A client program that executes a single HTTP GET to a Web server to retrieve any type of content from any website. A client program to execute a single HTTP GET to a Web server to retrieve a complete file, from any website, and store it locally. Write a client program to do serial HTTP GETs (that is, a sequence of GETs) to a Web server.

Solution

#include <string.h>
#include <stdio.h>
#include <winsock2.h>
#define BUFFER_LEN (4096)
using std::string;
int main(int argc, char **argv)
{
HANDLE fhand;
string request;
int sendret;
int iRecv;
int iResponseLength=0;
int offset;
DWORD dw;
string res2;
char recvBuffer[BUFFER_LEN]={0};
string response;
const char lb[]=\"\ \ \ \ \";
const char http[]=\"http\\x3a//\";
const char snsn[]=\"%s\ \";
bool error1=false;
bool error2=false;
bool error3=false;
int len3=strlen(argv[3]);
if(argc!=4)
{
printf(snsn,\"\ Invalid usage\");
goto cleanup;
}
WSADATA wsaData;
if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
{
printf(snsn,\"\ Error initializing Winsock 2.2\");
goto cleanup;
}
error1=true;
if(LOBYTE(wsaData.wVersion)!=2||HIBYTE(wsaData.wVersion)!=2)
{
printf(snsn,\"\ Winsock 2.2 not available\");
goto cleanup;
}
printf(snsn,\"\ Winsock 2.2 initialized via wsa2_32.dll\");
struct hostent *h;
struct sockaddr_in sa;
SOCKET server1;
h=gethostbyname(argv[1]);
if(h==0)
{
printf(snsn,\"\ gethostbyname() failed\");
goto cleanup;
}
printf(\"%s\",\"\ Host lookup succeeded for \");
printf(snsn,argv[1]);
memcpy((char *)&sa.sin_addr,(char *)h->h_addr,sizeof(sa.sin_addr));
sa.sin_family=h->h_addrtype;
sa.sin_port=htons(80);
server1=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(server1==INVALID_SOCKET)
{
printf(snsn,\"\ socket() failed\");
goto cleanup;
}
error1=false;
error2=true;
if(connect(server1,(struct sockaddr *)&sa,sizeof(sa))<0)
{
printf(snsn,\"\ connect() failed\");
goto cleanup;
}
printf(\"%s\",\"\ Now connected to \");
printf(\"%s\",argv[1]);
printf(snsn,\" via port 80\");
request+=\"GET \";
request+=argv[2];
request+=\" HTTP/1.0\";
request+=&lb[2];
request+=\"Host: \";
request+=argv[1];
request+=lb;
printf(snsn,\"\ HTTP request constructed successfully:\ \");
printf(snsn,request.c_str());
sendret=send(server1,request.c_str(),request.length(),0);
if(sendret==-1)
{
printf(snsn,\"send() failed\");
goto cleanup;
}
printf(snsn,\"Successfully sent HTTP request to the server\");
printf(snsn,\"\ Waiting for download to complete\");
   fhand=CreateFile(argv[3],GENERIC_WRITE,FILE_SHARE_READ,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
   if(fhand==INVALID_HANDLE_VALUE)
   {
printf(snsn,\"\ CreateFile() failed\");
goto cleanup;
   }
error2=false;
error3=true;
while((iRecv=recv(server1,recvBuffer,BUFFER_LEN-1,0))>0)
{
/*
char hex[5];
string packet;
packet.reserve(5*iRecv);
printf(snsn,\"\ \");
printf(\"%s\",\"Receiving \");
printf(\"%d\",iRecv);
printf(snsn,\" byte packet:\ \");
for(int i=0;i<iRecv;++i)
{
wsprintf(hex,\"%02x\",(unsigned char)recvBuffer[i]);
packet.append(hex);
printf(\"%s \",hex);
}
*/
response.append(recvBuffer,iRecv);
iResponseLength+=iRecv;
ZeroMemory(recvBuffer,BUFFER_LEN);
}
if(iRecv==SOCKET_ERROR)
{
printf(snsn,\"\ \ recv() failed\");
}
offset=response.find(lb)+4;
if(offset!=string::npos)
{
printf(\"%s\",\"\ \ File starts at offset \");
printf(\"%d\ \",offset);
printf(snsn,\"\ Initial response from server:\ \");
for(int j=0;j<offset;++j)
{
printf(\"%c\",response[j]);
}
res2.assign(response,offset,response.size());
if(WriteFile(fhand,res2.data(),res2.size(),&dw,0)==0)
{
printf(snsn,\"\ WriteFile() failed\");
goto cleanup;
}
else
{
printf(\"%s\",\"\ File successfully downloaded and saved to \");
printf(snsn,argv[3]);
}
}
cleanup:
if(error1)
{
WSACleanup();
}
if(error2)
{
WSACleanup();
closesocket(server1);
}
if(error3)
{
WSACleanup();
closesocket(server1);
CloseHandle(fhand);
}
return 0;
}

 The Hypertext Transfer Protocol HTTP is the foundation protocol of the World Wide Web and can be used in any client/server application involving hypertext. HTT
 The Hypertext Transfer Protocol HTTP is the foundation protocol of the World Wide Web and can be used in any client/server application involving hypertext. HTT
 The Hypertext Transfer Protocol HTTP is the foundation protocol of the World Wide Web and can be used in any client/server application involving hypertext. HTT

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site