I want to save everything received with GET request to file with this command:
./client http://example.com/1.jpg > image.jpg
My recv_buf
is 2000 bytes.
// Generate HTTP request
sprintf(send_buf, "%s %s %s%s %s%s", "GET", path, "HTTP/1.1\r\n",
"HOST:", hostname, "\r\n\r\n");
send_len = strlen(send_buf); //length of request to send
bytes_send = send(sockfd, send_buf, send_len, 0);
if (bytes_send <= 0)
{
perror("nothing sended\n");
exit(1);
}
printf("Bytes send: %d\nSended message: %s\n", bytes_send, send_buf);
// receive data
do {
bytes_recv = recv(sockfd, recv_buf, sizeof(recv_buf), 0);
if (bytes_recv <= 0)
{
perror("connection closed or error");
close(sockfd);
freeaddrinfo(results);
exit(0);
}
printf("Bytes received: %d\n", bytes_recv);
//write(1, recv_buf, bytes_recv);
} while (bytes_recv > 0);
I am so far that I can receive data till the end, but I don't know how to combine all the "packages" to one buffer in correct order and then save it to file as binary data (image).
Output is:
./client https://example.com/1.jpg
Bytes received: 2000
Bytes received: 1792
Bytes received: 2000
Bytes received: 1792
Bytes received: 639
connection closed or error: Success