寻找一个最简单(也是最快)的 Windows、C 或 c++ 的 TCP 套接字编程示例;

发布于 2024-09-28 11:11:28 字数 545 浏览 4 评论 0原文

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

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

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

谢谢。

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.

I know that UDP would be much faster, but it needs to be reliable at the same time, hence I'm looking for 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.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

甩你一脸翔 2024-10-05 11:11:29

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

您必须添加自己的计时支持,但这对于任何可能的现成软件包都可能是正确的。

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.

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

清泪尽 2024-10-05 11:11:29

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

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

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

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.

嘿嘿嘿 2024-10-05 11:11:29

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

开源:

  1. OpenDDS - 基于 DDS 协议(非常高性能 - 用于潜艇、船舶控制系统等) 它们的实现比原始 boost 稍慢::asio,然而,就易用性和附加功能而言,很难被击败。
  2. ZeroMQ - 与DDS类似,但基于MQ协议,同样非常快(数百万条消息/秒),MQ建立了,但ZeroMQ还没有。
  3. AMQP - 我相信您将能够在这个领域从 Red Hat 找到一些东西,同样非常快,并且是一个新协议。

商业广告:

  1. Tibco RV:很难被击败,除了硬件供应商
  2. 29West - 硬件(和软件 - 我个人从未使用过它)
  3. Solace - 硬件
  4. Tervella - 硬件

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

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.

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

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

ま柒月 2024-10-05 11:11:29

我所知道的最小示例位于 Beej 指南中。

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

一桥轻雨一伞开 2024-10-05 11:11:29

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

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

无所的.畏惧 2024-10-05 11:11:29

我刚刚使用 socket++ 实现了一个网络解决方案,并且效果很好。我相信它是boost asio的基础,所以如果你不想安装所有的boost,你可以检查一下。

该库的要点是您可以在套接字中使用流,将数据发送到 std::cout 或 std::cerr 。

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

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.

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.

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).

山人契 2024-10-05 11:11:29

您可以查看推送框架

You can check Push Framework.

永言不败 2024-10-05 11:11:29

ucspi-tcp

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

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

ucspi-tcp

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文