简体   繁体   English

在Windows 7上模拟套接字连接重置/超时

[英]Simulate socket connection reset/timeout on Windows 7

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. 目标是验证当远程服务器的长期套接字连接在运行时重置或服务器变得不可用时,我的Java代码是否按预期方式运行。

I tried creating a firewall rule in Windows Firewall. 我尝试在Windows防火墙中创建防火墙规则。 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. 拉网线不是一种选择,因为我必须通过与运行我的代码的服务器的远程桌面连接来执行这些测试。

I realize that this isn't directly under Windows, but considering the nature of Java, it may still work. 我意识到这不是直接在Windows下,但考虑到Java的本质,它可能仍然有效。

There is a program called Netem under Linux, which considering your code is java, may run. 在Linux下有一个名为Netem的程序,考虑到你的代码是java,可能会运行。 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. 你可以在虚拟机中轻松运行Linux(比如免费的(如啤酒,或者你在英国享受的免费语音)VirtualBox),希望你的Java应用程序可以在其中运行。

May not be perfect, and sorry about a general lack of know-how, but it might work for what you need 可能并不完美,对于普遍缺乏专业知识感到抱歉,但它可能适用于您的需求

以编程方式将套接字读取超时设置为1毫秒,您将始终获得java.net.SocketTimeoutException。

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. 一个简单,丑陋和危险的方法是快速禁用然后通过VBScript和WMI启用正确的NetworkAdapter

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. 更强大但更难设置的解决方案是使用WAN仿真器来模拟数据包丢失,断开连接和高延迟。 Your best free option here is dummynet , though there are lots of expensive, user friendly solutions out there too. 这里你最好的免费选择是dummynet ,虽然那里也有许多昂贵,用户友好的解决方案。

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! 如果在Windows和Linux中有一种更简单的方法可以做到这一点而不需要FreeBSD启动光盘和带有双网卡的虚拟服务器,我也想听听它!

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. 或者,如果由于某种原因,您无法终止您的客户端应用程序,则只需删除此代理应用程序,而不是以编程方式模拟已删除的连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM