Question: Help with the following C++ program using Visual Studio. Please use socket programming to do this task. Below is the code I have written that
Help with the following C++ program using Visual Studio. Please use socket programming to do this task. Below is the code I have written that is not yet complete.

#include
#include
#include
using namespace std;
#pragma comment (lib, "Ws2_32.lib")
#define BUFFERSIZE 513
int main()
{
//Declare and initialize variables
WSADATA wsaData;
int iResult;
struct addrinfo hints, *result;
char hostname[NI_MAXHOST] = "www.msu.edu";
SOCKET sock; //socket handle
char sendBuffer[BUFFERSIZE];
char recvBuffer[BUFFERSIZE];
//Initialize Winsock library
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0)
{
printf("WSAStartup failed: %d ", iResult);
return -1;
}
//Fill out the hints address info structure which will be passed to the getaddrinfo function as an argument
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
iResult = getaddrinfo(hostname, "80", &hints, &result);
if (iResult != 0)
{
printf("getaddrinfo failed: %d ", iResult);
WSACleanup();
return -1;
}
sock = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (sock == INVALID_SOCKET)
{
printf("socket failed with error: %d ", WSAGetLastError());
WSACleanup();
return -1;
}
iResult = connect(sock, result->ai_addr, (int)result->ai_addrlen);
if (iResult == SOCKET_ERROR)
{
closesocket(sock);
WSACleanup();
return 1;
}
//Prepare an http request
strcpy_s(sendBuffer, "GET / HTTP/1.1 ");
strcat_s(sendBuffer, "Host: ");
strcat_s(sendBuffer, hostname);
strcat_s(sendBuffer, " ");
strcat_s(sendBuffer, " "); //Terminates the string
//Sending the request
iResult = send(sock, sendBuffer, strlen(sendBuffer), 0);
if (iResult == SOCKET_ERROR)
{
closesocket(sock);
WSACleanup();
return 1;
}
printf("Total bytes sent: %d ", iResult);
int recvlen = BUFFERSIZE;
//Receiving data from the server
do
{
iResult = recv(sock, recvBuffer, recvlen - 1, 0);
if (iResult > 0){
recvBuffer[iResult] = '\0';
printf("%s", recvBuffer);
}
}
while (iResult > 0);
printf("%n");
closesocket(sock);
WSACleanup();
system("pause");
return 0;
}
Write a Windows Socket program that is capable of requesting and receiving the default HTML page from a web server. The program should prompt the user to input the web server hostname (for example www.msu.edu) from the keyboard and print the web server IP address as well as the total number of bytes received. The program may NOT print the HTML page. The request should be formatted as followed: "GET/HTTP/1.1 ln" "Host:
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
