用C程序语言模拟丢包

时间:2014-04-14 10:41:54

标签: c udp

我有用C编写的udp服务器/客户端代码,我想模拟数据包丢失,因为我的无线网络上的数据包丢失太高而且我无法执行测试,所以我切换到以太网。例如,我希望有20%的数据包丢失,我怎么能在C中做到这一点?

THX

1 个答案:

答案 0 :(得分:2)

我不确定您是否要在应用程序中模拟网络中断,因为测试只会显示模拟的工作原理。您必须模拟应用程序之外的数据包丢失。

您可以添加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/