在 Windows 7 上模拟套接字连接重置/超时
我想模拟套接字连接变坏以测试我的应用程序。目标是验证当与远程服务器的长期套接字连接在运行时重置或服务器变得不可用时,我的 Java 代码是否按照预期的方式运行。
我尝试在 Windows 防火墙中创建防火墙规则。但是,启用该规则似乎不会影响已打开的套接字连接。拉动网络电缆不是一个选项,因为我必须通过远程桌面连接到运行我的代码的服务器来执行这些测试。
I would like to simulate a socket connection going bad in order to test my application. The goal is to verify whether my Java code behaves the way it expected to, when a long standing socket connection to a remote server gets reset at runtime or server becomes unavailable.
I tried creating a firewall rule in Windows Firewall. However, enabling the rule does not seem to affect a socket connection that is already open. Pulling the network cable is not an option because I have to perform these tests over a remote desktop connection to the server that is running my code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我意识到这并不直接在 Windows 下,但考虑到 Java 的性质,它可能仍然有效。
Linux下有一个叫Netem的程序,考虑到你的代码是java,可以运行。我从未使用过它,所以我无法证明它的功能或用途,但有一些使用示例 这里。您可以相当简单地在 VM 中运行 Linux(就像免费的(如啤酒中的那样,或者像您在英国享受的自由言论一样)VirtualBox),并希望在其中运行 Java 应用程序。
可能并不完美,对于普遍缺乏专业知识感到抱歉,但它可能适合您的需要
I realize that this isn't directly under Windows, but considering the nature of Java, it may still work.
There is a program called Netem under Linux, which considering your code is java, may run. I've never used it, so I can't attest to its abilities or use, but there are some examples for use here. You can fairly trivially run Linux inside a VM (like the free (as in beer, or as in the kind of free speech you enjoy in Britain) VirtualBox) and hopefully your Java app inside that.
May not be perfect, and sorry about a general lack of know-how, but it might work for what you need
一种简单、丑陋且危险的方法是快速禁用然后启用正确的 NetworkAdapter 通过 VBScript 和 WMI。
更强大但设置起来稍微困难的解决方案是使用 WAN 模拟器来模拟数据包丢失、断开连接和高延迟。这里最好的免费选择是dummynet,尽管有很多昂贵的、用户友好的解决方案也在那里。
如果有一种更简单的方法可以在 Windows 和 Linux 中执行此操作,而不需要 FreeBSD 启动光盘和带双网卡的虚拟服务器,我也很想听听!
A simple, ugly and dangerous way of doing this would be to quickly disable and then enable the right NetworkAdapter via a VBScript and WMI.
A more robust, yet slightly harder to set up solution would be to use a WAN emulator to simulate packet loss, disconnects and high latency. Your best free option here is dummynet, though there are lots of expensive, user friendly solutions out there too.
If there's a simpler way to do this in Windows and Linux without needing FreeBSD boot discs and virtual servers with dual network cards, I'd also like to hear it!
您可以直接终止客户端应用程序。
此外,您可以通过为此创建一个非常简单的代理应用程序来实现此目的,该应用程序通常仅转发数据,但您可以对其进行编程以向其发出命令来执行各种其他操作。或者,如果由于某种原因您无法终止客户端应用程序,则只需终止此代理应用程序即可,而不是以编程方式模拟断开的连接。
You could just kill the client application.
Also, you could do this by creating a very simple proxy application for this which normally just forwards the data, but which you can program to give it commands to do various other things. Or, instead of programmatically simulating the dropped connection, just kill this proxy app if, for some reason, you cannot kill your client app.
以编程方式将套接字读取超时设置为 1 毫秒,您将始终收到 java.net.SocketTimeoutException。
Set the socket read timeout programmatically to 1 milliseconds and you will always get a java.net.SocketTimeoutException.