开发者

Simulate socket connection reset/timeout on Windows 7

开发者 https://www.devze.com 2023-01-24 10:31 出处:网络
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 be开发者_开发技巧haves the way it expected to, when a long standing so

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 be开发者_开发技巧haves 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.


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


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.


Set the socket read timeout programmatically to 1 milliseconds and you will always get a java.net.SocketTimeoutException.

0

精彩评论

暂无评论...
验证码 换一张
取 消