Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用C语言模拟丢包_C_Udp - Fatal编程技术网

用C语言模拟丢包

用C语言模拟丢包,c,udp,C,Udp,我有用C编写的udp服务器/客户端代码,我想模拟数据包丢失,因为我的无线网络上的数据包丢失太高,我无法执行测试,所以我切换到以太网。例如,我想有20%的数据包丢失,我如何在C中做到这一点 Thx我不确定您是否希望在应用程序中模拟网络中断,因为测试只会显示模拟有效。您必须在应用程序之外模拟数据包丢失 您可以添加iptables规则,以便: # for randomly dropping 20% of incoming packets: iptables -A INPUT -m statistic

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


Thx

我不确定您是否希望在应用程序中模拟网络中断,因为测试只会显示模拟有效。您必须在应用程序之外模拟数据包丢失

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

很难说没有看到您的代码,但您可以更改接收数据包的函数。如果
rand()%100>80
,则忽略一个数据包。这是一个很好的技巧,谢谢@免费昵称:这是19%。无论如何,
rand()
可能已经够糟糕了,这不重要。@Deduplicator,是的,我知道)我知道
rand()
的随机性质量很差,但我认为这并不重要。我只是觉得在这种情况下没那么重要。不过,我同意,
=80
更准确:)它是UDP,所以在应用程序内部是可以的。