How to download any file from internet only using C++ Standard library (no third party library)?

I know it may be tough to do so. But I was looking for if any guide exists to do that. May be I have to write that function from scratch to do that.
Currently, the standard C++ library does not provide any facilities for network operations.

The Technical Specification C++ Extensions for Networking is now published (ISO/IEC TS 19216:2018). It is expected that this extension would become a part of the standard in a future revision of the standard.

I do not know if any current implementation of the standard library provides this optional interface.
I believe the tech spec is an adoption of boost asio, which is not something you would use for interacting with HTTP (there are examples of hosting a server, but it isn't the tool for the job of being a client, unless you go down and gritty).

I think you have no choice but to use a library because that is just how c++ works when you want things done.

libcurl is the tool for doing http stuff.

https://stackoverflow.com/questions/1636333/download-file-using-libcurl-in-c-c

If you really don't want to deal with a C FILE, either find a C++'ified version of curl that does the same job, or wrap FILE in your own class and make the constructor and destructor do the work for you, (and if you are using a GNU compiler, you can use a hack called __gnu_cxx::stdio_filebuf but it is only useful if you really want a C++ stream for some reason).
An alternative on Windows is URLDownloadToFile.
this would be very (a year+ ?) difficult to do from scratch for a beginner or even intermediate programmer. Just talking to a network card (hardware or OS/driver interface) is needed, then you have to be able to process some protocols (TCPIP headers and low level communications stuff) and the high level protocols (FTP/HTTP/HTTPS/etc). You may need to handle some standard encryption/handshaking/other as well. You are going to have to talk to the OS at some level and doing that without a library is going to be somewhat involved.

Clearly it can be done. Each piece of a library that you would use here has been written in code and if it can be done, c++ can do it. But each piece was probably done by at least a small team of programmers with a lot of knowledge in that specific area, so while from scratch in code, they had a background in what needed doing that I certainly don't have and would venture a guess you don't either? I don't want to discourage you, but at the same time, at least realize the scope of the problem and that when its all done you will have spent years to build a program that you could have assembled in days using the available tools.

Someone has to build these tools. So if that is your passion, learn about it. But I would pick one piece of it to work on, and not try to go from end to end on the first try. A year to learn how to do this as a means to a career in the area is a good investment. A year to recreate the wheel, maybe not.
Last edited on
poteto wrote:
I believe the tech spec is an adoption of boost asio, which is not something you would use for interacting with HTTP


Hold my beer

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <boost/asio.hpp>
namespace ip = boost::asio::ip;
int main()
{
    ip::tcp::iostream s("ai.eecs.umich.edu", "http"); 
    s << "GET /people/dreeves/Fox-In-Socks.txt HTTP/1.0\r\n"
      << "Host: ai.eecs.umich.edu\r\n" 
      << "Accept: */*\r\n"
      << "Connection: close\r\n\r\n";
    std::cout << s.rdbuf();
}


But it's true, asio is too low-level to conveniently work with HTTP - that's why boost has boost.beast
Last edited on
Topic archived. No new replies allowed.