C# Simulate TCP port not responding
I have a proxy server written in C# that runs as a Windows Service. It listens on 127.0.0.1:xxxxx where xxxxx can be a range of ports from 53500 up.
The proxy works really well but sometimes certain ports stop responding and when I try to telnet to them it says that the request timed out or it did not respond.
I have not been able to reproduce this problem in my development area but I think it has to do with something causing BeginReceive to not be called after EndReceive. The following quote is from a thread about the symptoms I am experiencing.
Similar problem is when using asynchronous communication and there is an execution path which turns out to not call BeginReceive once previous EndReceive was completed. This makes socket ignorant to all further data sent by the remote side.
Is there a way to simulate this situation where a port stops responding. I want to write s开发者_StackOverflow中文版ome code to check if the port is responding before it proceeds with the request. If it is not responding I can remove the listener on that port and add it again. This is a band-aid to keep the proxy from crashing until I can determine the cause of the problem.
I've also added some extra logging to determine what is causing the ports to stop responding.
I'm taking a stab in the dark, but is the code wrapped in a try finally block?
If a finally block is the only place were you call EndReceive, it might eliminate your problem.
try{
// processing code
}finally{
EndReceive()
BeginReceive()
}
精彩评论