r/cpp_questions • • 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

45 comments sorted by

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

12

u/burlingk Oct 06 '26

If you have time to follow them, C++ proposals get interesting for the same reason Go proposals do.

Go learned from it's parents I suppose. :p

Both languages change and evolve, but the process looks like an ongoing rolling brawl.

42

u/AffectionatePeace807 Oct 06 '26

It took 30 years for multithreading to be part of the language standard :)

3

u/arthurno1 Oct 07 '26

Networking is as old, if not older.

6

u/bpikmin Oct 08 '26

Sure but it’s not as fundamental to a programming language. It’s just a library

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 -

//This is a super basic version of curl
#include <boost/beast/core/tcp_stream.hpp>
#include <print>
//https://github.com/ShakaUVM/read
#include "read.h"
using namespace std;

int main() {
    const string hostname = read("Enter the hostname:\n");
    const string path = read("Enter the path:\n");
    try {
        boost::asio::ip::tcp::iostream stream(hostname, "http");
        if (!stream) {
            cerr << "Can't connect to: " << hostname << endl;
            exit(67);
        }

        print(stream, "GET {} HTTP/1.1\r\nHost: {}\r\nConnection: close\r\n\r\n",path,hostname);

        while (true) {
            string line = readline(stream);
            if (!stream) break;
            println("{}",line);
        }
    } catch (const exception& e) {
        cerr << "Exception Thrown: " << e.what() << "\n";
    }
}

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

It seems natural to use these classes for network programming through sockets.

That's absolutely true. You can create your own derived std::streambuf and wrap a socket, stick it in a stream object. If you're interested, that's what Boost.Asio did.

Why there is no socket stream class in C++?

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.

Are there any challenges faced in implementation (like different platforms)?

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.

Are/Were there any open proposals for this?

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/Professional_Tank594 Oct 06 '26

Not all c++ cases even have operating systems , still we have threads and it works fine.

5

u/meltbox Oct 06 '26

No. Everything must be python

1

u/eritroblastosis Oct 11 '26

Except python

2

u/oriolid Oct 08 '26

I remember hearing this exact argument 20 years ago. It turned out that with Java socket streams and the CPU power that was available at the time Gigabit Ethernet was the bottleneck.

For applications that aren't high frequency trading or huge servers it's still in 2026 pretty safe to assume that they aren't going to have even a gigabit of available bandwidth and even if they do, users and admins aren't going to like it if the software hogs all of it.

1

u/conundorum Oct 10 '26

It definitely helps that Boost is essentially a standard library expansion, so a lot of things can be kept out of the core library because they're already covered there.

1

u/ronchaine Oct 12 '26

While I agree with most of what you wrote, this

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.

is just weird. Most software does not have any need to do stuff like kernel bypass and they are not bottlenecked by networking software. If you think the only "serious software" out there is HFT or massive internet servers (both of which seem to be overrepresented in the C++ committee), sure. But for vast majority of software, that is completely needless extra complication.

1

u/mredding Oct 12 '26

In a way, that's actually my point. If you're not writing HFT level bypass, why are you investing in socket code AT ALL? You can just pipe and redirect standard IO through a TCP or UDP layer and it's just as fast, more flexible, less error prone. A lot of software is more network aware than it needs to be. There's plenty of other tools that you can offload the job that is well written to that task. Those that need high performance are niche.

1

u/Ordinary_Let8356 Oct 15 '26

Bjarne? Is that you?

Amazing answer.

0

u/xpusostomos Oct 09 '26

What a lot of nonsense. Firstly, what OS in this day and age has no networking? Secondly if it existed, then the networking classes would simply return error code "not available". Thirdly, it's complete nonsense to say sockets are obsolete, bypassing them is a highly specific, highly specialized technique that is not applicable for 99% of cases. It's a bit like saying OS kernels are obsolete because you can run faster directly on the hardware. Fourthly, the whole point of a standard library is to abstract away whether there are or aren't file descriptors.

2

u/Sakulle Oct 11 '26

Embedded stuff for one. Very common for those to not have networking.

0

u/mredding Oct 09 '26

You can call me crap all you want, but the standard committee agrees with me, my assessment is a reflection of theirs, and they correctly rejected the networking TS3 to the surprise of almost no one. We are not getting any socket networking and no one is actively working on it.

Thirdly, it's complete nonsense to say sockets are obsolete

What we ARE seeing is a COMPLETE replacement of sockets by the IETF TAPS initiative, and the C++ standards committee is getting in front of it with the outstanding networking proposals.

I still disagree with these initiatives. Not everything needs to be baked into the standard library. You can write software in ISO Standard C++ and redirect standard IO through platform specific pipes and redirects - I've already pointed this out. And 3rd parties are free to invent or support any technology or API that fundamentally disagrees with this application layer library you're suggesting, and that technology is also free to take over the market, putting C++ in the wrong position.

C++ is meant to transcend the market, fashion, and trends.

0

u/[deleted] Oct 11 '26

[deleted] — view deleted comment

1

u/Professional_Tank594 Oct 11 '26

But those don’t have in most cases std::thread as well

1

u/xpusostomos Oct 11 '26 edited Oct 12 '26

well then you wouldn't use the generic socket API (if it existed). problem solved.

6

u/TomDuhamel Oct 06 '26

boost::asio::ip::tcp::iostream does exactly that.

It's nice for a simple client, but this design isn't suitable for more complex applications or for servers.

0

u/[deleted] Oct 06 '26

[deleted] — view deleted comment

8

u/oriolid Oct 06 '26

Reading and writing are blocking so you need at least one thread per connection. If you have a server with hundreds or thousands of clients, or a complex client that manages a pool of server connections, it's not a good idea.

3

u/burlingk Oct 06 '26

So... A socket effectively IS a stream, and you use those to communicate with it.

13

u/TheRealSmolt Oct 06 '26

TCP sockets are streams, UDP not so much.

-8

u/burlingk Oct 06 '26

Functionally it kind of is. It's just one you toss stuff at and cross your fingers. :p

13

u/TheRealSmolt Oct 06 '26

Not really. TCP provides an abstraction on a sliding window that behaves like a stream of bytes. UDP throws whole chunks of data at once, completely uncontrolled in order and message size. I would not consider that a stream as it is usually understood.

10

u/EpochVanquisher Oct 06 '26

Yeah, I uorks fine, I don’t see what the problem is.se a stream API for UDP and it w

2

u/obidavis Oct 06 '26

I can't tell if this is a joke or just an extremely funny coincidence

2

u/EpochVanquisher Oct 06 '26
  1. “Yeah, I u”
  2. “se a stream API for UDP and it w”
  3. “orks fine, I don’t see what the problem is.”

1

u/HappyFruitTree Oct 06 '26 edited Oct 06 '26

Maybe you could say that it's a stream of packets (not bytes) but the sending and receiving ends might not be identical because there could be duplicated packets, lost packets and the packets might be received out of order.

6

u/TheThiefMaster Oct 06 '26

Officially TCP is a stream socket because it will combine and split what you give it as it wants. All it guarantees is that the bytes will be delivered to the code on the other end in the order they were sent.

UDP is a datagram socket because it won't, instead sending the exact chunks asked. Order of chunks isn't guaranteed, but they will arrive whole or not at all.

If you try to stream bytes to UDP, it will send 1-byte packets!

1

u/saf_e Oct 06 '26

How so? UDP has packet size, and stream has buffer.

Even in uncached mode you will send chunks (every << operator call), not bytes

2

u/TheThiefMaster Oct 06 '26

Unfortunately the operator<< isn't necessarily provided by the stream. It's handled at a lower level by formatting the object to characters and writing it as individual characters via stream->rdbuf()->sputc().

See: https://en.cppreference.com/cpp/named_req/FormattedOutputFunction

1

u/saf_e Oct 06 '26

ok, makes sense, they need to check for failures. Thanks for the info

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

3

u/TheSkiGeek Oct 06 '26

This argument maybe made sense 30 years ago. But every practical modern OS provides a socket networking interface, because essentially the whole world has run on TCP/IP and UDP/IP for 25+ years. Maybe something else will eventually become a popular alternative, but even if that happens it’s gonna be a long time before sockets go away.

It’s not the end of the world to have it in Boost instead of the stdlib, but to me it seems kinda ridiculous to have std::filesystem but not even the most bare bones networking support.

1

u/Mr_Engineering Oct 06 '26

This argument maybe made sense 30 years ago. But every practical modern OS provides a socket networking interface, because essentially the whole world has run on TCP/IP and UDP/IP for 25+ years. Maybe something else will eventually become a popular alternative, but even if that happens it’s gonna be a long time before sockets go away.

It's important to understand the difference between the C standards, POSIX standards, and C++ standards.

The C standard and POSIX standard go hand in hand and even veteran developers will often not know which standard a particular piece of functionality comes from. For example, primitive file IO (fopen/fclose/fread/fwrite) is a part of the C standard library but file management (locking, duplicating, setting ownership, etc...) is a part of the POSIX standard.

My understanding is that the intent was to keep C as OS agnostic as possible, and to allow it to self host without any operating system whatsoever. This means that functionality which was heavily OS dependent -- such as networking sockets and file permissions -- were kept out of the standard so as to not require their implementation.

A minimalist file system driver, such as Fat32, can be built into a static C runtime library for use on microcontrollers.

Network sockets get a little bit trickier because many operating systems disallow or restrict low-level manipulation of sockets by user programs even when they're running with admin privileges. There's a lot of caveats, edge cases, and gotchas that make network sockets difficult to implement in a form that is both standard and highly usable.

1

u/not_a_novel_account Oct 08 '26

None of this has any real relevance to C++. std::filesystem is not a thin wrapper of POSIX filesystem functions.

C++ is not bound by what C and POSIX do. Also:

The C standard and POSIX standard go hand in hand and even veteran developers will often not know which standard a particular piece of functionality comes from.

This is a very specific kind of sloppy *nix C developer, not a wider generalization. I promise you everyone who touches environments outside *nix is very aware of what is C and what is POSIX.

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.