{"id":3040,"date":"2022-09-09T12:49:12","date_gmt":"2022-09-09T07:49:12","guid":{"rendered":"https:\/\/www.edopedia.com\/blog\/?p=3040"},"modified":"2022-09-09T12:49:14","modified_gmt":"2022-09-09T07:49:14","slug":"c-socket-program-to-download-file-from-url-over-http-protocol","status":"publish","type":"post","link":"https:\/\/www.edopedia.com\/blog\/c-socket-program-to-download-file-from-url-over-http-protocol\/","title":{"rendered":"C Socket Program to Download File From URL Over HTTP Protocol"},"content":{"rendered":"\n<p>In this tutorial, you will learn how to download a file from a URL over the HTTP protocol using C Socket Program. The complete C programming language source code for this project is given below.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">code.c<\/h2>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#include &lt;sys\/socket.h&gt;\n#include &lt;sys\/types.h&gt;\n#include &lt;netinet\/in.h&gt;\n#include &lt;netdb.h&gt;\n#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;unistd.h&gt;\n#include &lt;errno.h&gt;\n#include &lt;string.h&gt;\n#include &lt;errno.h&gt;\n#include &lt;arpa\/inet.h&gt;\n\n#include &lt;string.h&gt;\n\n\nint ReadHttpStatus(int sock){\n    char c;\n    char buff[1024]=&quot;&quot;,*ptr=buff+1;\n    int bytes_received, status;\n    printf(&quot;Begin Response ..\\n&quot;);\n    while(bytes_received = recv(sock, ptr, 1, 0)){\n        if(bytes_received==-1){\n            perror(&quot;ReadHttpStatus&quot;);\n            exit(1);\n        }\n\n        if((ptr[-1]=='\\r')  &amp;&amp; (*ptr=='\\n' )) break;\n        ptr++;\n    }\n    *ptr=0;\n    ptr=buff+1;\n\n    sscanf(ptr,&quot;%*s %d &quot;, &amp;status);\n\n    printf(&quot;%s\\n&quot;,ptr);\n    printf(&quot;status=%d\\n&quot;,status);\n    printf(&quot;End Response ..\\n&quot;);\n    return (bytes_received&gt;0)?status:0;\n\n}\n\n\/\/the only filed that it parsed is 'Content-Length' \nint ParseHeader(int sock){\n    char c;\n    char buff[1024]=&quot;&quot;,*ptr=buff+4;\n    int bytes_received, status;\n    printf(&quot;Begin HEADER ..\\n&quot;);\n    while(bytes_received = recv(sock, ptr, 1, 0)){\n        if(bytes_received==-1){\n            perror(&quot;Parse Header&quot;);\n            exit(1);\n        }\n\n        if(\n            (ptr[-3]=='\\r')  &amp;&amp; (ptr[-2]=='\\n' ) &amp;&amp;\n            (ptr[-1]=='\\r')  &amp;&amp; (*ptr=='\\n' )\n        ) break;\n        ptr++;\n    }\n\n    *ptr=0;\n    ptr=buff+4;\n    \/\/printf(&quot;%s&quot;,ptr);\n\n    if(bytes_received){\n        ptr=strstr(ptr,&quot;Content-Length:&quot;);\n        if(ptr){\n            sscanf(ptr,&quot;%*s %d&quot;,&amp;bytes_received);\n\n        }else\n            bytes_received=-1; \/\/unknown size\n\n       printf(&quot;Content-Length: %d\\n&quot;,bytes_received);\n    }\n    printf(&quot;End HEADER ..\\n&quot;);\n    return  bytes_received ;\n\n}\n\nint main(void){\n\n    char domain[] = &quot;sstatic.net&quot;, path[]=&quot;stackexchange\/img\/logos\/so\/so-logo-med.png&quot;; \n\n    int sock, bytes_received;  \n    char send_data[1024],recv_data[1024], *p;\n    struct sockaddr_in server_addr;\n    struct hostent *he;\n\n\n    he = gethostbyname(domain);\n    if (he == NULL){\n       herror(&quot;gethostbyname&quot;);\n       exit(1);\n    }\n\n    if ((sock = socket(AF_INET, SOCK_STREAM, 0))== -1){\n       perror(&quot;Socket&quot;);\n       exit(1);\n    }\n    server_addr.sin_family = AF_INET;     \n    server_addr.sin_port = htons(80);\n    server_addr.sin_addr = *((struct in_addr *)he-&gt;h_addr);\n    bzero(&amp;(server_addr.sin_zero),8); \n\n    printf(&quot;Connecting ...\\n&quot;);\n    if (connect(sock, (struct sockaddr *)&amp;server_addr,sizeof(struct sockaddr)) == -1){\n       perror(&quot;Connect&quot;);\n       exit(1); \n    }\n\n    printf(&quot;Sending data ...\\n&quot;);\n\n    snprintf(send_data, sizeof(send_data), &quot;GET \/%s HTTP\/1.1\\r\\nHost: %s\\r\\n\\r\\n&quot;, path, domain);\n\n    if(send(sock, send_data, strlen(send_data), 0)==-1){\n        perror(&quot;send&quot;);\n        exit(2); \n    }\n    printf(&quot;Data sent.\\n&quot;);  \n\n    \/\/fp=fopen(&quot;received_file&quot;,&quot;wb&quot;);\n    printf(&quot;Recieving data...\\n\\n&quot;);\n\n    int contentlengh;\n\n    if(ReadHttpStatus(sock) &amp;&amp; (contentlengh=ParseHeader(sock))){\n\n        int bytes=0;\n        FILE* fd=fopen(&quot;test.png&quot;,&quot;wb&quot;);\n        printf(&quot;Saving data...\\n\\n&quot;);\n\n        while(bytes_received = recv(sock, recv_data, 1024, 0)){\n            if(bytes_received==-1){\n                perror(&quot;recieve&quot;);\n                exit(3);\n            }\n\n\n            fwrite(recv_data,1,bytes_received,fd);\n            bytes+=bytes_received;\n            printf(&quot;Bytes recieved: %d from %d\\n&quot;,bytes,contentlengh);\n            if(bytes==contentlengh)\n            break;\n        }\n        fclose(fd);\n    }\n\n\n\n    close(sock);\n    printf(&quot;\\n\\nDone.\\n\\n&quot;);\n    return 0;\n}<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to download a file from a URL over the HTTP protocol using C Socket Program. The complete C programming language source code for this project is given below. code.c<\/p>\n","protected":false},"author":1,"featured_media":1762,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[112],"tags":[],"class_list":["post-3040","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>C Socket Program to Download File From URL Over HTTP Protocol<\/title>\n<meta name=\"description\" content=\"In this tutorial, you will learn how to download a file from a URL over the HTTP protocol using C Socket Program. The complete C programming language\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.edopedia.com\/blog\/c-socket-program-to-download-file-from-url-over-http-protocol\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C Socket Program to Download File From URL Over HTTP Protocol\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you will learn how to download a file from a URL over the HTTP protocol using C Socket Program. The complete C programming language\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.edopedia.com\/blog\/c-socket-program-to-download-file-from-url-over-http-protocol\/\" \/>\n<meta property=\"og:site_name\" content=\"Edopedia\" \/>\n<meta property=\"article:author\" content=\"trulyfurqan\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-09T07:49:12+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-09-09T07:49:14+00:00\" \/>\n<meta name=\"author\" content=\"Furqan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Furqan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C Socket Program to Download File From URL Over HTTP Protocol","description":"In this tutorial, you will learn how to download a file from a URL over the HTTP protocol using C Socket Program. The complete C programming language","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.edopedia.com\/blog\/c-socket-program-to-download-file-from-url-over-http-protocol\/","og_locale":"en_US","og_type":"article","og_title":"C Socket Program to Download File From URL Over HTTP Protocol","og_description":"In this tutorial, you will learn how to download a file from a URL over the HTTP protocol using C Socket Program. The complete C programming language","og_url":"https:\/\/www.edopedia.com\/blog\/c-socket-program-to-download-file-from-url-over-http-protocol\/","og_site_name":"Edopedia","article_author":"trulyfurqan","article_published_time":"2022-09-09T07:49:12+00:00","article_modified_time":"2022-09-09T07:49:14+00:00","author":"Furqan","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Furqan","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.edopedia.com\/blog\/c-socket-program-to-download-file-from-url-over-http-protocol\/#article","isPartOf":{"@id":"https:\/\/www.edopedia.com\/blog\/c-socket-program-to-download-file-from-url-over-http-protocol\/"},"author":{"name":"Furqan","@id":"https:\/\/www.edopedia.com\/blog\/#\/schema\/person\/3951cb19e3aa56df09e408c98aa02339"},"headline":"C Socket Program to Download File From URL Over HTTP Protocol","datePublished":"2022-09-09T07:49:12+00:00","dateModified":"2022-09-09T07:49:14+00:00","mainEntityOfPage":{"@id":"https:\/\/www.edopedia.com\/blog\/c-socket-program-to-download-file-from-url-over-http-protocol\/"},"wordCount":48,"commentCount":0,"publisher":{"@id":"https:\/\/www.edopedia.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.edopedia.com\/blog\/c-socket-program-to-download-file-from-url-over-http-protocol\/#primaryimage"},"thumbnailUrl":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/02\/default_featured_image.jpg","articleSection":["Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.edopedia.com\/blog\/c-socket-program-to-download-file-from-url-over-http-protocol\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.edopedia.com\/blog\/c-socket-program-to-download-file-from-url-over-http-protocol\/","url":"https:\/\/www.edopedia.com\/blog\/c-socket-program-to-download-file-from-url-over-http-protocol\/","name":"C Socket Program to Download File From URL Over HTTP Protocol","isPartOf":{"@id":"https:\/\/www.edopedia.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.edopedia.com\/blog\/c-socket-program-to-download-file-from-url-over-http-protocol\/#primaryimage"},"image":{"@id":"https:\/\/www.edopedia.com\/blog\/c-socket-program-to-download-file-from-url-over-http-protocol\/#primaryimage"},"thumbnailUrl":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/02\/default_featured_image.jpg","datePublished":"2022-09-09T07:49:12+00:00","dateModified":"2022-09-09T07:49:14+00:00","description":"In this tutorial, you will learn how to download a file from a URL over the HTTP protocol using C Socket Program. The complete C programming language","breadcrumb":{"@id":"https:\/\/www.edopedia.com\/blog\/c-socket-program-to-download-file-from-url-over-http-protocol\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.edopedia.com\/blog\/c-socket-program-to-download-file-from-url-over-http-protocol\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.edopedia.com\/blog\/c-socket-program-to-download-file-from-url-over-http-protocol\/#primaryimage","url":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/02\/default_featured_image.jpg","contentUrl":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/02\/default_featured_image.jpg","width":880,"height":495,"caption":"Default Featured Image"},{"@type":"BreadcrumbList","@id":"https:\/\/www.edopedia.com\/blog\/c-socket-program-to-download-file-from-url-over-http-protocol\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.edopedia.com\/blog\/"},{"@type":"ListItem","position":2,"name":"C Socket Program to Download File From URL Over HTTP Protocol"}]},{"@type":"WebSite","@id":"https:\/\/www.edopedia.com\/blog\/#website","url":"https:\/\/www.edopedia.com\/blog\/","name":"Edopedia","description":"Coding\/Programming Blog","publisher":{"@id":"https:\/\/www.edopedia.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.edopedia.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.edopedia.com\/blog\/#organization","name":"Edopedia","url":"https:\/\/www.edopedia.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.edopedia.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2017\/10\/edopedia_icon_text_10.jpg","contentUrl":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2017\/10\/edopedia_icon_text_10.jpg","width":400,"height":100,"caption":"Edopedia"},"image":{"@id":"https:\/\/www.edopedia.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.edopedia.com\/blog\/#\/schema\/person\/3951cb19e3aa56df09e408c98aa02339","name":"Furqan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/e5e68aef3ad8f0b83d56f4953c512c8e57bd2e6dc64daec33b5d0495d9058f51?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/e5e68aef3ad8f0b83d56f4953c512c8e57bd2e6dc64daec33b5d0495d9058f51?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e5e68aef3ad8f0b83d56f4953c512c8e57bd2e6dc64daec33b5d0495d9058f51?s=96&d=mm&r=g","caption":"Furqan"},"description":"Well. I've been working for the past three years as a web designer and developer. I have successfully created websites for small to medium sized companies as part of my freelance career. During that time I've also completed my bachelor's in Information Technology.","sameAs":["http:\/\/www.edopedia.com\/blog\/","trulyfurqan"],"url":"https:\/\/www.edopedia.com\/blog\/author\/furqan\/"}]}},"_links":{"self":[{"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/posts\/3040","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/comments?post=3040"}],"version-history":[{"count":1,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/posts\/3040\/revisions"}],"predecessor-version":[{"id":3041,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/posts\/3040\/revisions\/3041"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/media\/1762"}],"wp:attachment":[{"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/media?parent=3040"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/categories?post=3040"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/tags?post=3040"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}