问题
How can my test program cause a "connection was forcibly closed" error in the TCP listener it’s connected to?
My understanding is that I’d have to close the connection without following the TCP protocol, namely without sending a "FIN" packet. Can this be done with a standard .NET Socket in TCP mode?
I seem to be able to do this by opening several connections and then terminating the test program, but not in any other way I’ve tried.
回答1:
I was able to accomplish this by using the utility CurrPorts.
For my purposes, I was attempting to test a crash within a third-party SDK that was caused by this error.
- Run CurrPorts in Administrator mode
- Hit Refresh when you see the connection.
- Right-click and "Close Selected TCP Connections" (or use ctl-T)
回答2:
You should not be able to achieve this by opening several connections and then terminating the test program. The kernel should take care of closing all of the TCP connections (cleanly, at least at the layer of TCP and below) when the program exits.
In fact, if there is anything at all that you can do from unprivileged userspace to cause a hanging/broken TCP connection, it's a bug in your kernel and should be fixed :-)
Your best option is probably to use raw sockets to inject whatever packets you want. You will have to build your own TCP & IP headers on these packets, but for purposes of unit testing you can build headers out of mostly fixed values with a few variant values thrown in. A suggest starting with a packet capture of a normal, working session, add a RST packet at the right place, and more or less play back that sequence in your test program.
I do think that doing this to test your application is overkill. You're mostly testing the hosts's TCP stack with this approach. To test your application, mocking the socket so that it returns an error at the right time is probably good enough.
回答3:
Depending on your code environment I see several possibilities.
One way would be to implement an Adapter pattern as described in this SO question here: TDD and Mocking out TcpClient. (See there for code examples.)
You could implement a wrapper class that itself makes use of the TcpClient and the mock the interface of that wrapper class. Doing it this way decouples you from the TcpClient and makes changing to other implementations or prtocols if needed.
The second idea would be to use a mocking framework like Moq, RhinoMock (both open source) or a commercial one like Telerik's JustMock (my personal preference). JustMock also offers a free edition that would well suit this task and may also be used in commeccial projects. Using such a framework then enables you to easily mock out the public interface of your network classes.
Of course it would also make sense to use a mocking framework for the first option, too.
Either way, the mocks then can be arranged to throw the desired exceptions and test how your code bahaves to it.
回答4:
You can download a tcp monitoring util from Microsoft site. The util name is TcpView and let you monitor and close your tcp connections to simulate this condition.
TcpView:
https://technet.microsoft.com/en-us/sysinternals/tcpview.aspx
来源:https://stackoverflow.com/questions/9925705/how-to-emulate-a-forcibly-closed-tcp-connection