寻找一个最简单(也是最快)的 Windows、C 或 c++ 的 TCP 套接字编程示例;
我正在寻找一个最简单(也是最快)的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
尝试 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.
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.
如果您想要现成的产品,请查看任何可用的消息传递产品。它们需要最少的编码才能运行,典型的例子是:
开源:
商业广告:
最后三个假设你有几百万美元! ;)
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:
Commercial:
The last three assumes you've got a few million bucks lying around! ;)
我所知道的最小示例位于 Beej 指南中。
The most minimal examples of which I am aware are in Beej's Guide.
在写第三条评论之前,我将它们收集在一个答案中
有 RUDP,它可靠且快速,因为它省略了连接设置: en.wikipedia.org/wiki/Reliable_User_Datagram_Protocol ;另请参阅当您需要可靠的 UDP 时,您会使用什么?
出自 Steven 的《UNIX 网络编程 I》,第 14 页。 369 我建议至少为 FreeBSD 实现 T/TCP: http://www. manpages.info/freebsd/ttcp.4.html
Before writing the third comment, I collect them in an answer
There's RUDP which is reliable and fast since it omits the connection setup: en.wikipedia.org/wiki/Reliable_User_Datagram_Protocol ; see also What do you use when you need reliable UDP?
Out of Steven's UNIX Network Programming I, p. 369 I suggest T/TCP which is implemented at least for FreeBSD: http://www.manpages.info/freebsd/ttcp.4.html
我刚刚使用 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).
您可以查看推送框架。
You can check Push Framework.
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