C++ http GET request using sockets

Hello

I am trying to communicate with a server that is ready to accept HTTP GET requests and send back data per the request. However, I have very little experience in socket programming and I don't really know how to debug this. Googling on the web hasn't yielded much, except people saying I need to read the HTTP 1.1 spec which is the last thing I want to do.

I seem to be able to create, bind the socket and then send a GET request and that seems ok too but I am not able to receive anything.

The following are the relevant parts of the code:

Connection established:

/* connect to host */
 if(connect(hSocket,(struct sockaddr*)&Address,sizeof(Address)) 
    == SOCKET_ERROR)
 {
   verbose("Could not connect to host a socket");
  return 0;
 }
 
 verbose("Connection established") 

Sending request:

char *getRequest = "GET /path/to/server.aspx?Action=GetConfig&MachineName=node1 HTTP/1.1\n";
 
 if(send(hSocket, getRequest, strlen(getRequest), 0) < 0) {
  verbose("Error with sending socket");
  return 0;
 }
 else {
  verbose("Successful with sending socket get request to get config file");
 }

The above works fine, but then below the receiving fails, in the first if-statement:

verbose("Attempting to receive from server");
 char echoBuffer[9999];
 
 /* Receive config information from server */ 
    int totalBytesRcvd = 0; 
    unsigned int echoStringLen;
    int bytesRcvd;
    printf("Received: ");                /* Setup to print the echoed string */ 
    while (totalBytesRcvd < echoStringLen) 
    { 
        /* Receive up to the buffer size (minus 1 to leave space for 
           a null terminator) bytes from the sender */ 
        if ((bytesRcvd = recv(hSocket, echoBuffer, 9999 - 1, 0)) <= 0) 
        {
            printf("recv() failed or connection closed prematurely"); 
            return 0;
        }
        totalBytesRcvd += bytesRcvd;   /* Keep tally of total bytes */ 
        echoBuffer[bytesRcvd] = '\0';  /* Terminate the string! */ 
        printf("%s", echoBuffer);      /* Print the echo buffer */ 
    } 

Can anyone share some insight as to how I can re-write the receiving code to get the data that is supposed to be returned to be based on the GET request? What is wrong here?

Thanks for the help.

Try understanding what you are supposed to get. Do a dummy session with telnet:
HTTP Help: How to test HTTP using Telnet

Once you know:

  1. the connection I am using works
  2. what data to expect back

Then you can adjust your code.

Thank you so much, this is a great way to test. I shouldn't be using the HTTP/1.1\n at the end.