为 windows、c 或 c++ 寻找最简单(和最快)的 TCP 套接字编程示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3978952/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:26:55  来源:igfitidea点击:

Looking for a simplest (and fastest) example of TCP socket programming for windows, c or c++

c++windowsperformancesocketswinsock

提问by ra170

I'm looking for a simplest (and fastest) example of TCP socket programming for windows, c or c++, whichever can get it accomplished faster, sending trival data, for example 1 byte, or several bytes, but in one packet. It's for research purposes. I googled and found several examples, however every single of out them looks a bit different, some are in C, some are in C++, some use ZeroMemory (from windows), some use memset, some of them assign data in different ways, so while I can find examples of winsock in c/c++ and while I'm not an expert in socket programming - I'm not sure what's the absolutely minimalistic c/c++ code to get it accomplish in a fastest way possible.

我正在为 windows、c 或 c++ 寻找一个最简单(和最快)的 TCP 套接字编程示例,无论哪个可以更快地完成它,发送琐碎的数据,例如 1 个字节或几个字节,但在一个数据包中。这是为了研究目的。我用谷歌搜索并找到了几个例子,但是它们中的每一个看起来都有点不同,有些是用 C 语言,有些是在 C++ 中,有些使用 ZeroMemory(来自 Windows),有些使用 memset,其中一些以不同的方式分配数据,所以虽然我可以在 c/c++ 中找到 winsock 的例子,虽然我不是套接字编程的专家 - 我不确定什么是绝对简约的 c/c++ 代码来让它以最快的方式完成。

I know that UDP would be much faster, but it needs to be reliable at the same time, hence I'm looking for TCP.

我知道 UDP 会快得多,但同时它需要可靠,因此我正在寻找 TCP。

I guess I could try each of them and try to time them, but was wondering if some socket/winsock expert here would have a super simple server/client in C/C++ with some timing function (high resolution) at the end. I say super simple, because I'm trying to determine how fast (and the fastest way) can socket transmit on my machines, of course it can include turning off Nagle's algorithm, which is what I would like to do anyway. I'm not sure what other tricks people use.

我想我可以尝试每一个并尝试为它们计时,但想知道这里的某些套接字/winsock 专家是否会在 C/C++ 中拥有一个超级简单的服务器/客户端,最后带有一些计时功能(高分辨率)。我说超级简单,因为我正在尝试确定我的机器上套接字传输的速度(和最快的方式),当然它可以包括关闭 Nagle 的算法,无论如何,这就是我想要做的。我不确定人们使用的其他技巧。

Thanks.

谢谢。

采纳答案by Peter G.

Before writing the third comment, I collect them in an answer

在写第三条评论之前,我将它们收集在一个答案中

回答by Steve Townsend

Try Len Holgate's socket server framework. I believe he has commercialized this in a packaged version but this should be a good place to start. There is a client implementation tutorial included. This is not the simplest code but if you are interested in maximizing performance, simple code may not meet your needs.

试试 Len Holgate 的套接字服务器框架。我相信他已经在打包版本中将其商业化,但这应该是一个很好的起点。包含一个客户端实现教程。这不是最简单的代码,但如果您对最大化性能感兴趣,简单的代码可能无法满足您的需求。

You will have to add your own timing support, but that's likely true for any possible off-the-shelf package.

您将不得不添加自己的计时支持,但对于任何可能的现成软件包来说都可能是这样。

回答by Kyle C

Boost Asiois probably your best bet. it's a very good library with timing support and everything you should need to get going.

Boost Asio可能是您最好的选择。这是一个非常好的库,具有计时支持以及您开始所需的一切。

edit: I know that this isn't a pre-built client/server which is exactly what you are looking for, but Asio makes it extremely easy to get what you want out of a few lines of code.

编辑:我知道这不是您正在寻找的预先构建的客户端/服务器,但是 Asio 使从几行代码中获得您想要的东西变得非常容易。

回答by Billy ONeal

The most minimal examples of which I am aware are in Beej's Guide.

我所知道的最简单的例子在Beej's Guide 中

回答by Nim

If you want an off the shelf product, look at any of the messaging products available. They require the least amount of coding to get going, typical examples are:

如果您想要现成的产品,请查看任何可用的消息传递产品。它们需要最少的编码才能开始,典型的例子是:

Open Source:

开源:

  1. OpenDDS - based on the DDS protocol (very high performance - used in things like submarine, ship control systems etc.) Their implementation is slightly slower than raw boost::asio, however for ease of use and the bells and whistles, hard to beat.
  2. ZeroMQ - similar to DDS, but based on the MQ protocol, again very fast (millions of messages/sec), MQ is established, but ZeroMQ is not there yet.
  3. AMQP - I believe you'll be able to find something from Red Hat in this space, again very fast, and a new protocol.
  1. OpenDDS - 基于 DDS 协议(非常高性能 - 用于潜艇、船舶控制系统等)它们的实现比原始 boost::asio 稍慢,但是为了易于使用和花里胡哨,很难被击败.
  2. ZeroMQ - 类似于 DDS,但基于 MQ 协议,同样非常快(数百万条消息/秒),MQ 已建立,但 ZeroMQ 尚未存在。
  3. AMQP - 我相信你将能够在这个领域从 Red Hat 中找到一些东西,同样非常快,而且是一种新的协议。

Commercial:

商业的:

  1. Tibco RV: hard to beat, except by hardware vendors
  2. 29West - hardware (and software - thought I've never personally played with it)
  3. Solace - hardware
  4. Tervella - hardware
  1. Tibco RV:难以击败,硬件供应商除外
  2. 29West - 硬件(和软件 - 以为我从未亲自玩过它)
  3. 安慰 - 硬件
  4. Tervela - 硬件

The last three assumes you've got a few million bucks lying around! ;)

最后三个假设你有几百万美元!;)

回答by charfeddine.ahmed

You can check Push Framework.

您可以检查Push Framework

回答by Jorge Ramirez

ucspi-tcp

ucspi-tcp

Oldie but goodie, written in C, qmail is widely used mail server is based on it.

老掉牙的好东西,用C写的,qmail被广泛使用的邮件服务器就是基于它的。

https://cr.yp.to/ucspi-tcp.html

https://cr.yp.to/ucspi-tcp.html

回答by mmr

I've just implemented a network solution using socket++, and it works pretty well. I believe that it's the basis for boost asio, so if you don't want to install all of boost, you can check it out.

我刚刚使用socket++实现了一个网络解决方案,它运行良好。我相信它是boost asio的基础,所以如果你不想安装所有的boost,你可以看看。

The point of the library is that you can use a stream with your socket, sending data as you would to std::cout or std::cerr.

该库的重点是您可以在套接字中使用流,像向 std::cout 或 std::cerr 一样发送数据。

EDIT: if you're using more recent versions of windows, then this library would need some tweaking to compile (it works fine as-is for XP, but apparently some networking code got moved around for win vista and 7).

编辑:如果您使用的是更新版本的 windows,那么这个库需要一些调整才能编译(它在 XP 中可以正常工作,但显然一些网络代码在 win vista 和 7 中移动了)。