Urgent problem with An existing connection was forcibly closed by the remote host
Hi,
I have an urgent problem need help. We have an Asp.net application runing on window 2003 server. One part of the application is socket programming. When customer hits our site a little bit heavier, after serveral thoudsands hits, I statrt to get a bunch of
An existing connection was forcibly closed by the remote host
exceptions, then if the customer continue hits the site, the other function throw out of memory exception, The result is our site not responding any more. The attached is the socket call programming, any help will be appreciated! Thank you.
IPAddress[] ips = Dns.GetHostAddresses(m_sAVSHost);
IPEndPoint oIPEndPoint = new IPEndPoint(ips[0], iAvsPort);
Socket oSocket = new Socket(oIPEndPoint.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
try
{
oSocket.Connect(oIPEndPoint);
byte[] bSendBytes = Encoding.UTF8.GetBytes(sAvsParam);
oSocket.Send(bSendBytes, bSendBytes.Length, SocketFlags.None);byte[] bRecvBytes = new byte[4096];
oSocket.Receive(bRecvBytes, SocketFlags.None);
rtnVal = Encoding.UTF8.GetString(bRecvBytes).Trim();
}
catch (Exception ex)
{
VVXmlListenerException.LogException(new VVXmlListenerException(ex));
}
finally
{
oSocket.Shutdown(SocketShutdown.Both);
oSocket.Close();
}
Answers
I guess this is because the number of clients exceed the backlog set in the listen request of the server.
Please take a look at (5) & (6) th points in
https://blogs.msdn.com/malarch/archive/2006/06/26/647993.aspx.
Try to avoid starting many clients at the same time.. One simple solution is to Add a random sleep before starting the clients.
Malar
Customer now resends the data when the other side closes the connection
Mariya
All Replies
I would run a packet trace to find out what's actually going over the network. (Use a tool like Ethereal or Netmon.)
On all the occasions I've seen this exception, it has meant exactly what it says: the connection was forcibly closed. So this suggests that whatever your code here is connecting to is closing the connection. (Or you're going through a firewall, proxy, or other intermediary which is closing it.) The first step would be to confirm that - look at the actual packets going across the network to see what's really happening - that will most likely confirm that the error you're getting is consistent with the incoming network packets.
And if that's what you see, then the problem is external to the code you're showing us here. You need to look into why the external system - the thing that this code connects to - is kicking you off.
What does this code connect to? (I.e. what is m_sAVSHost?)
- For instructions on how to get a netmon trace see here:For instructions on how to get a System.Net tracing log go here:Mariya
I guess this is because the number of clients exceed the backlog set in the listen request of the server.
Please take a look at (5) & (6) th points in
https://blogs.msdn.com/malarch/archive/2006/06/26/647993.aspx.
Try to avoid starting many clients at the same time.. One simple solution is to Add a random sleep before starting the clients.
Malar
Customer now resends the data when the other side closes the connection
Mariya
- I am living diffuculty at the same problem if you have solved the problem can you help me please? Thank you in advance
The problem is caused by the remote port not being available. You get a ICMP packet with "Destination unreachable (Port unreachable)" and that causes an exception when you try to "listenerPort.Receive" The error is generated by sending data to an unreachable port and then windows receiving an ICMP packet with the error. The packet is in the receive buffer and when you try to read it you end up with an exception. All you have to do is add a TRY and then igonore the error and go back to listening to the port.
You do not get an exception when you send but that is where the problem lies.
Raymond Ortiz
- Proposed As Answer bykhanfauji Wednesday, June 03, 2009 5:20 PM


