我有用 C 编写的 udp 服务器/客户端代码,我想模拟数据包丢失,因为无线网络上的数据包丢失太高,我无法执行测试,因此我切换到以太网。例如我想要 20% 的丢包率,我该如何在 C 中做到这一点?
谢谢
最佳答案
我不确定您是否想要在应用程序中模拟网络中断,因为测试只会显示模拟是否有效。您必须在应用程序外部模拟数据包丢失。
您可以添加 iptables 规则来执行此操作:
# for randomly dropping 20% of incoming packets:
iptables -A INPUT -m statistic --mode random --probability 0.2 -j DROP
# and for dropping 20% of outgoing packets:
iptables -A OUTPUT -m statistic --mode random --probability 0.2 -j DROP
http://code.nomad-labs.com/2010/03/11/simulating-dropped-packets-aka-crappy-internets-with-iptables/
关于c - C语言模拟丢包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23058019/