I'm sending and get request to a server and receiving the response back, problem is, I just need to grab the headers to check the status code but the recv function seems to be stuck in the previous response, so even though I've sent a new request, I still keep receiving the first response. Now, how do I reset the recv function and grab only the headers, dropping everything else?
#define BUF_LEN 100
bytes_received = 1;
while(bytes_received = recv(sock, &get_response_buffer, BUF_LEN, 0) > 1)
{
printf("\n----------RESPONSE-----------\n%s\n-----------------------------\n",get_response_buffer);
// bytes_received = write(sock, &get_response_buffer, BUF_LEN);
if(strstr(get_response_buffer, "HTTP/1.1 404 Not Found"))
{
printf("[404 NOT FOUND]\tGET %s%s\n",inet_ntoa(server_addr.sin_addr), read_string);
break;
}
else if(strstr(get_response_buffer, "HTTP/1.1 302 Found"))
{
printf("[302 FOUND]\tGET %s%s\n", inet_ntoa(server_addr.sin_addr), read_string);
break;
}
else if(strstr(get_response_buffer, "HTTP/1.1 200 OK"))
{
printf("[200 OK]\t GET %s%s\n", inet_ntoa(server_addr.sin_addr), read_string );
break;
}
else
{
puts("not OK");
break;
}
}
Like so, both responses are from the first get request. If I keep sending more requests I just keep receiving the rest of the first request.
----------RESPONSE-----------
HTTP/1.1 404 Not Found
Cache-Control: public, max-age=1
Content-Type: text/html; charset=utf-8
Ex
-----------------------------
----------RESPONSE-----------
pires: Thu, 19 Oct 2017 14:37:14 GMT
Last-Modified: Thu, 19 Oct 2017 14:37:13 GMT
Vary: *
X-AspNe
-----------------------------