我在我们的软件中有一个错误,当我收到连接超时时发生。这些错误是非常罕见的(通常是当我的连接被我们的内部网络丢弃时)。我怎样才能人为地产生这种效果来测试我们的软件呢?

如果重要,应用程序是用c++ /MFC编写的,使用CAsyncSocket类。

编辑:

我尝试使用一个不存在的主机,我得到套接字错误:

无效的参数

我的下一个尝试是使用Alexander的建议,连接到不同的端口,例如81(在我自己的服务器上)。这很有效。与断开连接完全相同(等待60秒,然后出错)。谢谢你!


当前回答

连接到一个现有的主机,但是连接到一个被防火墙阻塞的端口,防火墙只是丢弃TCP SYN包。例如:www.google.com:81。

其他回答

Depending on what firewall software you have installed/available, you should be able to block the outgoing port and depending on how your firewall is setup it should just drop the connection request packet. No connection request, no connection, timeout ensues. This would probably work better if it was implemented at a router level (they tend to drop packets instead of sending resets, or whatever the equivalent is for the situation) but there's bound to be a software package that'd do the trick too.

您可以尝试在外部端口(例如200)上连接到一个知名的Web站点。大多数防火墙都在DROP模式下工作,它会为你模拟超时。

您可以使用Python REPL来模拟接收数据时的超时(即在连接成功建立之后)。只需要一个标准的Python安装。

Python 2.7.4 (default, Apr  6 2013, 19:54:46) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)        
>>> s.bind(('localhost', 9000))
>>> s.listen(0)
>>> (clientsocket, address) = s.accept()

现在它等待一个传入连接。将您想要测试的任何东西连接到localhost:9000。当你这样做时,Python将接受连接,accept()将返回它。除非您通过客户端套接字发送任何数据,否则调用方的套接字应该在下一次recv()期间超时。

有很多好的答案,但最干净的解决方案似乎是这项服务

https://httpstat.us/504?sleep=60000

您可以配置超时时间(最多230秒)和最终返回代码。

我过去曾使用过一些策略来模拟网络问题;

拔出网线 关闭你的机器和“目标”机器之间的开关(理想情况下,计算机插入的开关仍在供电,以便机器保持“网络连接”) 在目标计算机上运行防火墙软件,以静默方式丢弃接收到的数据

其中一个想法可能会为您提供一些人为生成所需场景的方法