# C++ http GET request using sockets

**URL:** <https://community.unix.com/t/c-http-get-request-using-sockets/310590>\
**Category:** Programming\
**Created:** [May 21, 2012, 5:27pm UTC](https://community.unix.com/t/c-http-get-request-using-sockets/310590 "2012-05-21T17:27:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![flagman5](https://community.unix.com/letter_avatar/flagman5/32/6_22734026db12803ebb3e059622ac3534.png) [@flagman5](https://community.unix.com/u/flagman5)\
**Post date:** [May 21, 2012, 5:27pm UTC](https://community.unix.com/t/c-http-get-request-using-sockets/310590/1 "2012-05-21T17:27:20Z")

</div>

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:

```nohighlight
/* 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:

```nohighlight
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:

```nohighlight
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.

---

<div class="post-metadata">

**Author:** ![jim\_mcnamara](https://community.unix.com/letter_avatar/jim_mcnamara/32/6_22734026db12803ebb3e059622ac3534.png) [@jim\_mcnamara](https://community.unix.com/u/jim_mcnamara)\
**Post date:** [May 21, 2012, 5:52pm UTC](https://community.unix.com/t/c-http-get-request-using-sockets/310590/2 "2012-05-21T17:52:08Z")

</div>

Try understanding what you are supposed to get. Do a dummy session with telnet:  
[HTTP Help: How to test HTTP using Telnet](http://www.esqsoft.com/examples/troubleshooting-http-using-telnet.htm)

Once you know:

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

Then you can adjust your code.

---

<div class="post-metadata">

**Author:** ![flagman5](https://community.unix.com/letter_avatar/flagman5/32/6_22734026db12803ebb3e059622ac3534.png) [@flagman5](https://community.unix.com/u/flagman5)\
**Post date:** [May 23, 2012, 10:22am UTC](https://community.unix.com/t/c-http-get-request-using-sockets/310590/3 "2012-05-23T10:22:59Z")

</div>

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