views:

191

answers:

6

How are files downloaded from servers in programming languages like C? I understand higher level languages have magic functions like "download_file_from_url()" but they don't help me understand what is actually going on. I'm a little familiar with sockets but network programming in general is still a black box to me. Thanks for any help.

+7  A: 

You should check out libcurl - it's open source so you can dig through it and see how a respected library approaches the problem.

Hank Gay
A: 

Use a library like libcurl.

jkramer
+4  A: 

And a "black box" is probably a good way to keep it :-)

You do the same thing in C that you would do in "higher level languages" - use a library function that does it for you. (The difference is that the library function isn't a standard built-in part of the language).

One choice for C is libcurl

David Gelhar
+12  A: 
T.J. Crowder
Your details are wrong. if HTTP/1.1 is specified the server should keep the connection alive, unless "connection: close" is passed as a header.
KillianDS
@KillianDS: Thanks! Fixed.
T.J. Crowder
+1  A: 

If you are downloading a file using HTTP then you should read RFC on HTTP (how data is split by chunks etc.), using FTP — RFC on FTP (which commands are used, e. g. PWD, CD etc.). However these are higher-level protocols that utilize sockets anyway.

Yasir Arsanukaev
+1  A: 

To download a file (assume a simple case - no firewall etc...), you need to:

  • Connect to a DNS server to resolve the name of the URL's server into an IP

  • Open a connection to that IP on the URL's port or default port for your protocol (80 for http)

  • Send the appropriate HTTP command over to that server

  • Listen for HTTP response

  • Process response correctly, and if the response contains the data for the file, keepr eding the reponse and saving the data in temp file

  • When file is fully downloaded, close the connection and move the complete temp file into proper location.

DVK
though I like the detail/specificity of your response - why include bullet #1 as this is automatic on nearly every imaginable system where this could be developed? Note too that you gave the 35,000 ft version, which is not at all specific to the OP's query.
KevinDTimm