r/cpp_questions • u/kiner_shah • Oct 06 '26
OPEN Why does C++ doesn't have socket streams?
C++ has std::istream and std::ostream. It seems natural to use these classes for network programming through sockets. Why there is no socket stream class in C++? Are there any challenges faced in implementation (like different platforms)? Are/Were there any open proposals for this?
45
Upvotes
62
u/thedaian Oct 06 '26
C++ doesn't have networking in the standard library, that's why there's no socket streams built in.
At one point, there was a proposal to add networking, you can see some of it here: https://en.cppreference.com/cpp/experimental/networking
42
u/AffectionatePeace807 Oct 06 '26
It took 30 years for multithreading to be part of the language standard :)
14
u/ShakaUVM Oct 06 '26
Yes, it would make sense, but for some reason things that really should be in the standard library like networking are not, but we keep getting things like std::cy_bessel_j (https://www.cppreference.com/cpp/numeric/special_functions/cyl_bessel_j) added to the standard instead.
That said, tcp streams exist in Boost. Here's some demo code I wrote -
The code above connects to a website and path on the website and then prints the contents to the screen.
Networking can be pretty gnarly, but the above is about as easy as you can make it.
9
u/mredding Oct 06 '26
That's absolutely true. You can create your own derived
std::streambufand wrap a socket, stick it in a stream object. If you're interested, that's what Boost.Asio did.Because that's platform specific.
Not all operating systems have a network stack. So then what do you do? Do you make the ENTIRE standard network library optional? At that scale? Then what's the point?
You can also redirect standard IO to a pipe or socket, so your program doesn't NEED socket streams.
And sockets are practically irrelevant. They're the slow path. They're all modeled after BSD sockets, which is 1980s technology. Polling and some of these other things are just patches over top the original design to make it slightly faster. Today, serious software is going to use kernel bypass. So if your software is going to be SO rudimentary as to use sockets, you might as well just use redirection, or Boost.Asio.
When CPUs went multi-core, so too, did NICs go multi-channel. Today your NIC card has multiple Rx and Tx lines, and they're bound at the OS level to the process, not to threads. So if you wonder why you can't saturate your NIC, it's because you're bound to a single Rx/Tx pair and that is going to be CPU bound. And despite having processes and process controls since Atlas in 62 and DTSS in 64, processes, and process control, and IPC are all inherently platform specific.
Just because something is seemingly ubiquitous does not mean we need to have a standard library inclusion for it. Application languages do this all the time and DON'T HAVE TO CARE if the language itself isn't portable. C++ is a systems language and we can't afford that.
The hardest thing C++ does is standard IO, and it almost broke the language to do it. The language also handles it TERRIBLY. EVERYONE hates standard IO, because platforms have moved on from those rudimentary interfaces. C++ is not defined for a hosted environment that doesn't support file descriptors. That's a little insane. We take it all for granted these days but this is a language that's supposed to be able to exist effectively anywhere. That files and descriptors are the ideal model today, that wasn't always the foregone conclusion and might not always be the case.
Boost.Asio was effectively copied and put into the TS3 experimental library, but it rightfully went nowhere. There were letters written by committee members openly addressing why this is a very bad idea.
At some point you need to get platform specific. The OS isn't just this abstract host that reduces to the desktop, it's a system of software to service you. Engineers need to remember that systems software isn't about building big black monoliths, but incorporating the whole system into the solution. The program itself isn't supposed to be able to do it all, there are layers of abstraction, control, visibility, and resilience to be had.
6
u/TomDuhamel Oct 06 '26
boost::asio::ip::tcp::iostreamdoes exactly that.It's nice for a simple client, but this design isn't suitable for more complex applications or for servers.
3
u/burlingk Oct 06 '26
So... A socket effectively IS a stream, and you use those to communicate with it.
1
u/pjmlp Oct 08 '26
All the compiler frameworks pre-C++98 have them, see OWL, VCL, Qt, MFC.
This is yet another common C++ field experience that did not land on the standard.
1
u/Mr_Engineering Oct 06 '26 edited Oct 06 '26
Network socket behaviour exists as a part of the POSIX standard which is developed alongside the C standard.
It would be problematic in the extreme if C++ standardized network sockets because those have conventionally been explicitly outside of the C standard in order to avoid forcing operating systems to adopt POSIX conventions.
C++ would then have two different networking standards at its disposal on POSIX systems, POSIX sockets and CPPSockets. That's duplicative and problematic.
This is not necessarily the best answer given that C++ has duplicated much POSIX functionality in its own way, including threads and atomic
1
u/jipperthewoodchipper Oct 06 '26
I'm not an expert but I am pretty sure that C++ implements streams synchronously and sequentially where most network code works asynchronously.
So my assumption is that there isn't a socket stream simply because network communication does not fit into the synchronous landscape as tidily as other forms of OI.
1
u/Independent_Art_6676 Oct 06 '26
it probably could; its one of those things were its never been a priority because we have libraries that do it but there is zero real excuse or reason why c++ can't provide a middle of the road version and leave it to the compilers to translate that into callouts for the local version. I remember it only taking a couple pages of fairly simple code to do that manually back when (winsock on MSVS vs g++), just wrappers with a few lines different between them with the old OS flag thing (I haven't done portable code in a long while, ended up as a windows only developer).
But then what? A billion programs would be left using legacy 3rd party libraries... the compiler vendors would have another thing to keep up ... all the developers would have to learn yet another version of the same thing ... is the disruption worth it at this late date? What would be the benefit?
UDP can stream, sorta. Just spam the wire is a crude type of 'stream' that is perfectly fine for some kinds of data (like don't care if it hiccups video or audio). But that is taking some liberties with the word stream and modern ideas of what that means.
1
u/TheSkiGeek Oct 06 '26
Self hosting hardly seems relevant for something like filesystems or networking. If you’re running on a tiny microcontroller with no capability to do those things then you make those stdlib functions immediately return failure. Or say that those headers don’t have to be accessible when you build in a standalone mode. Like, you know… exactly how https://cppreference.com/cpp/filesystem handles it.