使用C / C ++套接字模拟网络条件 [英] Simulate network conditions with a C/C++ Socket

查看:155
本文介绍了使用C / C ++套接字模拟网络条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来添加网络仿真到套接字。



基本的解决方案是添加带宽限制到连接。 p>

对我来说,理想的解决方案是:




  • 支持高级网络属性-loss)

  • 开放源代码

  • 拥有与标准套接字类似的API(或包围它们)

  • 在Windows和Linux上工作

  • 支持IPv4和IPv6



在系统级别上工作的选项,甚至作为代理(Dummynet,WANem,neten等),但是不会为我工作,因为我想要能够手动模拟每个套接字(例如,打开一个套接字



编辑:我需要嵌入这个功能在我自己的产品,因此使用一个额外的盒子或第三方工具,需要手动配置是不能接受的。我想编写与这些工具做同样的代码,我的问题是如何做。



结语: ,我的问题有点误导。显然,没有办法做我想直接在套接字上。有两种选择:



添加发送/接收操作的延迟(基于@ PaulCoccoli的回答):
urch
通过在发送和接收之前添加一个延迟,你可以得到一个非常粗糙的网络模拟(延迟的延迟,延迟发送,不发送多于X字节每秒,对于带宽)。 $ b $bÚ
保罗的回答和评论是我的灵感,所以我给他奖赏。



添加网络模拟逻辑作为代理(基于@ m0she和其他人回答):

通过代理发送请求,或使用代理拦截请求,然后添加所需的模拟。但是,使用现成的解决方案而不是编写自己的代理实现更有意义 - 从我看到的 Dummynet 可能是最好的选择(这是什么webpagetest.org)。其他选项在下面的答案中,我还将添加 DonsProxy
ought
这是更好的方法,所以我接受这个答案。

解决方案

你可以编译一个代理到你的软件,那。
它可以是一些实现完全成熟的socks代理(如)或可能更好,更简单的只服务于您的目的(并且不需要前缀您的通信与目的地和其他袜子开销)。
该代码可以作为一个单独的进程或进程内的线程运行。



向代理添加限制不应太难。您可以:




  • 如果数据通过某些带宽限制,则延迟数据转发


  • 如果您使用基于连接的协议(如TCP),丢弃数据包将毫无意义,但使用基于数据报的协议(UDP )



连接创建API与普通的posix / winsock有点不同一些宏或其他魔法),但一切(发送/ recv /选择/关闭/ etc ..)是一样的。


I'm looking for a way to add network emulation to a socket.

The basic solution would be some way to add bandwidth limitation to a connection.

The ideal solution for me would:

  • Support advanced network properties (latency, packet-loss)
  • Open-source
  • Have a similar API as standard sockets (or wraps around them)
  • Work on both Windows and Linux
  • Support IPv4 and IPv6

I saw a few options that work on the system level, or even as proxy (Dummynet, WANem, neten, etc.), but that won't work for me, because I want to be able to emulate each socket manually (for example, open one socket with modem emulation and one with 3G emulation. Basically I want to know how these tools do it.

EDIT: I need to embed this functionality in my own product, therefore using an extra box or a third-party tool that needs manual configuration is not acceptable. I want to write code that does the same thing as those tools do, and my question is how to do it.

Epilogue: In hindsight, my question was a bit misleading. Apparently, there is no way to do what I wanted directly on the socket. There are two options:

Add delays to send/receive operation (Based on @PaulCoccoli's answer):
by adding a delay before sending and receiving, you can get a very crude network simulation (constant delay for latency, delay sending, as to not send more than X bytes per second, for bandwidth).
Paul's answer and comment were great inspiration for me, so I award him the bounty.

Add the network simulation logic as a proxy (Based on @m0she and others answer):
Either send the request through the proxy, or use the proxy to intercept the requests, then add the desired simulation. However, it makes more sense to use a ready solution instead of writing your own proxy implementation - from what I've seen Dummynet is probably the best choice (this is what webpagetest.org does). Other options are in the answers below, I'll also add DonsProxy
This is the better way to do it, so I'm accepting this answer.

解决方案

You can compile a proxy into your software that would do that. It can be some implementation of full fledged socks proxy (like this) or probably better, something simpler that would only serve your purpose (and doesn't require prefixing your communication with the destination and other socks overhead). That code could run as a separate process or a thread within your process.

Adding throttling to a proxy shouldn't be too hard. You can:

  • delay forwarding of data if it passes some bandwidth limit
  • add latency by adding timer before read/write operations on buffers.
  • If you're working with connection based protocol (like TCP), it would be senseless to drop packets, but with a datagram based protocol (UDP) it would also be simple to implement.

The connection creation API would be a bit different from normal posix/winsock (unless you do some macro or other magic), but everything else (send/recv/select/close/etc..) is the same.

这篇关于使用C / C ++套接字模拟网络条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆