简体   繁体   English

C++ 套接字编程

[英]C++ Socket Programming

I'm wondering if there's any simple and clean way to create, write to, and receive from network sockets in C++.我想知道是否有任何简单而干净的方法可以在 C++ 中创建、写入和接收网络套接字。 I know in C you can use sys/sockets among other low-level libraries, but is there anything superior for C++?我知道在 C 中你可以在其他低级库中使用 sys/sockets,但是 C++ 有什么更好的吗? We're on C++-17 and yet I still can't find much about socket programming.我们在 C++-17 上,但我仍然找不到很多关于套接字编程的信息。 On Python it's easy, but C++ makes it intimidating for me.在 Python 上这很容易,但是 C++ 使我感到害怕。

The C++ standard library does not currently have any socket or network functionality. C++ 标准库当前没有任何套接字或网络功能。 The standards committee does have a networking subgroup, who are working on a Networking TS ( current draft ), but that didn't make it into C++17.标准委员会确实有一个网络子组,他们正在研究网络 TS( 当前草案),但这并没有进入 C++17。

For the time being, there's Boost.Asio , which the Networking TS is largely based upon.目前,有Boost.Asio ,Networking TS 主要基于它。 That provides a cross-platform networking interface.这提供了一个跨平台的网络接口。 Of course, you can also directly use your OS's networking APIs, such as POSIX sockets or WinSock.当然,您也可以直接使用操作系统的网络 API,例如 POSIX 套接字或 WinSock。

2020 2020年

Still nothing in the standard library.标准库中仍然没有。 However, there is a clean thin solution in the form of kissnet .但是,有一种以kissnet形式存在的干净的薄解决方案。

It's a thin wrapper (~1700 lines) around respective socket headers in Linux / Windows.它是 Linux / Windows 中相应套接字头周围的薄包装器(约 1700 行)。

Secure sockets via OpenSSL.通过 OpenSSL 保护套接字。

C++ 17 compiler is required, as they're using std::byte and other perks.需要 C++ 17 编译器,因为它们使用std::byte和其他特权。

Why isn't something like this in the standard library?为什么标准库中没有这样的东西? Beats me.打我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM