I've written my own C# TCP communications module (using SocketAsyncEventArgs, although that's presumably irrelevant). My module runs at both ends of the connection, client and server. As part of the programming it is supposed to detect when the connection fails, and then automatically try to reestablish the connection.

I'd like to hear idea about how to test this. One solution is to run it on two physical machines and unplug the network cable for a while and then reconnect. This isn't very automated.

I'm wondering if there is some kind of WinSock hook program that can be used to simulate connection failures? Or any other suggestions?

  • Welcome to Software Recommendations! Please note this site is about recommending software, not assets like howtos, manuals, multi-media content – so the "I'd like to hear idea about how to test this" part is off-topic here. Please also check with What is required for a question to contain "enough information"? to see if you can edit and improve your question for best results: the better you describe what you need, the better answers can match that :)
    – Izzy
    Commented Apr 17, 2015 at 13:43

2 Answers 2


TCP states are well-known but how they are handled on different clients, and or servers are not!....

TCP automatically attempts to retry up to a certain amount of time before it times-out, sends the proper responses and cleans up resources. This is only on one side whereby the stack has control. It's up to the other side to determine is a particular socket (session) should be closed.

There are things you can test knowing this information... One side starts a session and does nothing... (When will the other side send the RST or FIN). One side starts and does something but doesn't finish (When will the other side close?) One side starts and does something wrong, maybe sends a response that wasn't expected (again a test to see if the other side recovers)...

Knowing whether or not a particular application is in-session depends on the application under test. For Example: FTP always has two sockets one for control and the other for data transfer. FTP Stays read for additional data transactions as long as the control session is not closed.

There are two ways to detect if an application (on the network) is up and running. One is called Port Pinging. All this does is attempt to open a socket to the IP/Port (Socket). If it opens then the application is listening! The other is to open and then send data to see it the application is able to receive content. From there you have any number of permutations you can dream up to "really test" this thing.

So to test what you asked for automatically. You have to think like a tester... Start first with both sides up and running (Sunny day test). After first test passes run 2nd test which only opens connection and does nothing else... How long until other side times out? Now open connection send data for 3rd test but don't complete it properly, just stop sending data without closing? What happens? Now try to open a connection when the other side is not listening... Now try to bombard the other side with too many clients and data....

Fun indeed but it takes a lot of programming effort.

  • Thanks, you present some interesting ideas.
    – RenniePet
    Commented Apr 18, 2015 at 2:22

I don't think you need to purchase software for this. Add a separate thread in your code with access to the Socket object and just close the socket randomly at some point during the test window. Maybe go through the test scenario several times so that it disconnects at different points. The exception thrown may be different than the multitude of exceptions that would get thrown during the various types of actual socket disconnections, but unless you're looking at specific exceptions, it should be sufficient to test the code.

  • Thanks for answering. Just closing the socket might be feasible - I'll have to think about it
    – RenniePet
    Commented Apr 18, 2015 at 2:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.