使用 C++ 联网
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3847107/
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
Networking with C++
提问by TopGunCoder
I'm a newcomer to using C++ but have got a general Idea of its syntax and usability. I want to learn how to communicate over networks through C++ programming though but I can't seem to find any tutorials for C++ specifically. Does anyone know any good resources to learn about networking with C++ or what I should start with?
我是使用 C++ 的新手,但对它的语法和可用性有一个大致的了解。我想学习如何通过 C++ 编程通过网络进行通信,但我似乎找不到任何专门针对 C++ 的教程。有没有人知道任何好的资源来学习 C++ 网络或我应该从什么开始?
采纳答案by Steve Townsend
Given your newness to C++, I would not recommend building directly on the sockets APIs unless you can find no suitable library to use. Boost.Asiowill give you a huge head start and expose you to the higher-level abstractions used in network programming.
鉴于您对 C++ 不熟悉,我不建议直接在套接字 API 上构建,除非您找不到合适的库来使用。 Boost.Asio将为您提供一个巨大的开端,并让您接触网络编程中使用的更高级别的抽象。
It's easy when starting out building a sockets-based system to get something that 'sort of' works and then spend weeks debugging corner cases that only happen under real-world timing and load conditions. Using boost::asio
correctly is hardly a cakewalk even if it shields developers from the complexities of raw socket handling.
开始构建基于套接字的系统以获得“某种”工作的东西,然后花费数周时间调试仅在实际时序和负载条件下发生的极端情况时,这很容易。boost::asio
即使它使开发人员免受原始套接字处理的复杂性影响,正确使用也不是一件容易的事。
If the goal is to learn how to use raw sockets (or some other transport mechanism such as RPC) correctly, then by all means roll your own using online samples and docs to understand the individual BSD or Winsock APIs - if the goal is to solve a business problem as quickly as possible with high quality code on both business and networking infrastructure side, then use a good networking library. In this case your question does indicate a wish to learn so using a library may not be the best way to achieve your stated goal.
如果目标是学习如何正确使用原始套接字(或其他一些传输机制,如 RPC),那么一定要使用在线示例和文档来了解各个 BSD 或 Winsock API,如果目标是解决在业务和网络基础设施方面使用高质量代码尽快解决业务问题,然后使用良好的网络库。在这种情况下,您的问题确实表明希望学习,因此使用图书馆可能不是实现您既定目标的最佳方式。
回答by user225312
You should check out these guides about:
您应该查看以下指南:
And Boost.AsioLook herefor a question on documentation related to Boost.Asio
而Boost.Asio的看这里的相关Boost.Asio的文件上的一个问题
回答by asveikau
I'm sure a bunch of C++ people who despise the C way of doing things will hate me for this, but the classical approach is to use the Berkeley socket APIs(so-called because they have their origins in BSD). If you're targeting Windows, the "largely-source-compatible, inspired-by-Unix" APIs are called Winsock. I'd say do a web search for socket tutorialand you will probably get some useful information. With a little care and maybe an #ifdef
or two, it's not so hard to create code which works on Linux, BSD, Mac (which provide BSD sockets) and Windows (with Winsock) using what's common between these two interfaces.
我敢肯定,一群鄙视 C 做事方式的 C++ 人会为此讨厌我,但经典的方法是使用Berkeley 套接字 API(之所以这么称呼,是因为它们起源于 BSD)。如果您的目标是 Windows,那么“很大程度上与源代码兼容、受 Unix 启发”的 API 称为Winsock。我会说在网络上搜索套接字教程,你可能会得到一些有用的信息。稍微小心一点,也许一#ifdef
两个,使用这两个接口之间的共同点,创建适用于 Linux、BSD、Mac(提供 BSD 套接字)和 Windows(使用 Winsock)的代码并不难。
For the more C++ inclined, less C-style people, or those who don't like to code against OS APIs, I'm sure there are C++ libraries out there that provide wrappers and abstractions for these same concepts. Since Boost seems to be pretty popular, I'd say see what they have.
对于更倾向于 C++、较少 C 风格的人,或者那些不喜欢针对 OS API 进行编码的人,我确信有 C++ 库可以为这些相同的概念提供包装器和抽象。由于 Boost 似乎很受欢迎,我想说看看他们有什么。
回答by fschmitt
The standard library of C++ doesn't have support for this, so you either have to use the system API or some abstraction library, e.g. QT.
C++ 的标准库不支持这一点,因此您必须使用系统 API 或某些抽象库,例如 QT。
回答by bmeric
回答by Olupot Douglas
C++ is an Object Oriented language and open so you can borrow from other languages particularly C. but here are a few books that might help. http://wjyl.nuaa.edu.cn/kfyy/article/C++%20Network%20Programming%20Volume%201.pdfand perhaps http://www.cs.wustl.edu/~schmidt/PDF/ACE-tutorial.pdfamong many. there others out there as well.
C++ 是一种面向对象的语言并且是开放的,因此您可以借用其他语言,尤其是 C。但是这里有几本书可能会有所帮助。http://wjyl.nuaa.edu.cn/kfyy/article/C++%20Network%20Programming%20Volume%201.pdf或许还有http://www.cs.wustl.edu/~schmidt/PDF/ACE-tutorial。许多pdf。还有其他人在那里。
回答by 4xy
Perhaps Boost.Asioor this one (Unicomm)based on it will be suitable for you
也许Boost.Asio或基于它的这个(联通)会适合你