C++11 中会(并且应该)有套接字吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6638213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Will (and should) there be sockets in C++11?
提问by Touzen
Is the new C++11 going to contain any socket library? So that one could do something std::socket
-ish?
新的 C++11 会包含任何套接字库吗?这样一个人可以做某事std::socket
-ish?
Seeing as how std::thread
will be added, it feels as if sockets should be added as well. C-style sockets are a pain... They feel extremely counter-intuitive.
看怎么std::thread
加,感觉好像也要加socket。C 型套接字很痛苦……它们感觉非常违反直觉。
Anyways: Will there be C++ sockets in C++11 (googled it but couldn't find an answer)? If not, are their any plans on adding this? Why (/ why not)?
无论如何:C++11 中是否会有 C++ 套接字(用谷歌搜索但找不到答案)?如果没有,他们有没有计划添加这个?为什么(/为什么不)?
采纳答案by Nicol Bolas
No, it is not. As for the near future, the C++ standards committee has created a study group that is developing a networking layer proposal. It looks like they're going for a bottom-up approach, starting with a basic socket layer, then building HTTP/etc support on top of that. They're looking to present the basic socket proposal at the October committee meeting.
不它不是。至于不久的将来,C++ 标准委员会已经创建了一个研究组,正在开发网络层提案。看起来他们正在采用自下而上的方法,从一个基本的套接字层开始,然后在此之上构建 HTTP/etc 支持。他们希望在 10 月的委员会会议上提出基本的套接字提案。
As for why they didn't put this into C++11, that is purely speculative.
至于为什么他们没有把它放到 C++11 中,那纯属推测。
If you want my opinion on the matter, it's for this reason.
如果你想听听我对此事的看法,就是因为这个原因。
If you are making a program that does something, that has a specific functionality to it, then you can pick libraries for one of two reasons. One reason is because that library does something that is necessaryto implement your code. And the other is because it does something that is helpfulin implementing code in general.
如果您正在制作一个程序来执行某些操作,并且具有特定的功能,那么您可以出于以下两个原因之一选择库。一个原因是因为该库做了一些实现你的代码所必需的事情。另一个是因为它做了一些有助于实现代码的事情。
It is very difficult for a design for a particular program to say, "I absolutely must use a std::vector
to hold this list of items!" The designfor a program isn't that specific. If you're making a web browser, the idea of a browser doesn't care if it holds its tabs in a std::vector
, std::list
, or a user-created object. Now, some design can strongly suggest certain data structures. But rarely does the design say explicitly that something low-level like a std::list
is utterly essential.
一个特定程序的设计很难说:“我绝对必须使用 astd::vector
来保存这个项目列表!” 程序的设计并不是那么具体。如果你正在做一个网页浏览器,如果它保持其在标签浏览器的想法并不关心std::vector
,std::list
或者用户创建的对象。现在,某些设计可以强烈建议某些数据结构。但是设计很少明确说明像 a 这样低级的东西std::list
是绝对必要的。
std::list
could be used in just about anyprogram. As can std::vector
, std::deque
, etc.
std::list
几乎可以在任何程序中使用。正如std::vector
,std::deque
等等。
However, if you're making a web browser, bottled within that design is networking. You musteither use a networking library or write a networking layer yourself. It is a fundamental requirement of the idea.
但是,如果您正在制作 Web 浏览器,那么该设计中包含的内容是网络。您必须使用网络库或自己编写网络层。这是理念的基本要求。
The term I use for the former type, for libraries that could be used in anything, is "utility" libraries.
我对前一种类型使用的术语,即可以用于任何事物的库,是“实用程序”库。
Threading is a utility library. Design might encourage threading through the need to respond to the user, but there are ways to be responsive without preemptive multithreading. Therefore, in most cases, threading is an implementation choice. Threading is therefore a utility.
线程是一个实用程序库。设计可能会鼓励线程处理响应用户的需求,但是有一些方法可以在没有抢占式多线程的情况下做出响应。因此,在大多数情况下,线程是一种实现选择。因此线程是一种实用程序。
Networking is not. You only use networking if your design specifically calls for it. You don't decide to just dump networking into a program. It isn't an implementation detail; it is a design requirement.
网络不是。只有在您的设计特别需要时才使用网络。您不会决定将网络转储到程序中。这不是一个实现细节;这是一个设计要求。
It is my opinion that the standard C/C++ library should only implement utilities. It's also why I'm against other heavyweight ideas like XML parsers, etc. It isn't wrong for other libraries to have these things, but for C and C++, these are not good choices.
我认为标准 C/C++ 库应该只实现实用程序。这也是为什么我反对其他重量级的想法,比如 XML 解析器等。其他库拥有这些东西并没有错,但对于 C 和 C++ 来说,这些都不是很好的选择。
回答by myeviltacos
I think it should, since a lot of other popular languages support socket operations as a part of the language (they don't force the user to use any OS-specific API). If we already have file streams to read/write local files, I don't see why we can't have some method of transferring data with sockets.
我认为应该如此,因为许多其他流行语言都支持将套接字操作作为语言的一部分(它们不强制用户使用任何特定于操作系统的 API)。如果我们已经有文件流来读/写本地文件,我不明白为什么我们不能有一些使用套接字传输数据的方法。
回答by Puppy
There will be no sockets in C++11. The difference between threads and sockets is that threads involves making more guarantees about ordering, ifyour program involves threads. For a platform with just one core, then C++11 doesn't mandate that your CPU springs an extra core. Sockets, on the other hand, would be... difficult to implement portably and fail gracefully on systems that don't have them.
C++11 中将没有套接字。线程和套接字之间的区别在于,如果您的程序涉及线程,则线程涉及对排序做出更多保证。对于只有一个内核的平台,C++11 并不强制要求您的 CPU 弹出一个额外的内核。另一方面,套接字将......难以移植并在没有它们的系统上优雅地失败。
回答by Ben Voigt
There will not be in C++0x. There are proposals to add them in a future version.
在 C++0x 中不会有。有建议在未来版本中添加它们。
The amount of new stuff in C++0x had to be limited to give the committee time to deal with it all thoroughly.
必须限制 C++0x 中新内容的数量,以便委员会有时间彻底处理这一切。
回答by Andrew White
The wikipedia page for C++0x is usually pretty up to date and the section on library changesdoesn't seem to mention sockets.
回答by Jay
This is so weird that in 2020, there is still no standard for a basic OS construct as sockets in C++.
这太奇怪了,以至于在 2020 年,对于 C++ 中的套接字等基本操作系统构造仍然没有标准。
The closest I found is kissnet(Apparently exists since 2019).
我发现最接近的是kissnet(显然自2019年以来就存在)。
It's small (~1700 lines), runs on Windows and Linux, uses OpenSSL, and requires C++ 17 (Which is a plus in my book), basically everything I needed.
它很小(约 1700 行),在 Windows 和 Linux 上运行,使用 OpenSSL,并且需要 C++ 17(这是我书中的一个加分项),基本上是我需要的一切。