locked
Urgent problem with An existing connection was forcibly closed by the remote host

    Question

  • 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();
                    }

    Thursday, June 22, 2006 5:19 PM

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

     

    Tuesday, June 27, 2006 1:25 AM
  • Customer now resends the data when the other side closes the connection

    Mariya

    Wednesday, June 28, 2006 10:53 PM
    Moderator

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?)

    Friday, June 23, 2006 12:37 PM
  • 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
    Monday, June 26, 2006 5:44 PM
    Moderator
  • 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

     

    Tuesday, June 27, 2006 1:25 AM
  • Customer now resends the data when the other side closes the connection

    Mariya

    Wednesday, June 28, 2006 10:53 PM
    Moderator
  • I am living diffuculty at the same problem if you have solved the problem can you help me please? Thank you in advance
    Sunday, October 7, 2007 11:02 PM
  • 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

    fraggnet@excite.com

     

    • Proposed as answer by khanfauji Wednesday, June 3, 2009 5:20 PM
    Thursday, May 15, 2008 4:05 PM
  • Are you using a Server Farm or Network Load Balance, If yes. what are you using for? Usually  happens when the destination port is being  blocked with a firewall.

    Try to use TCP View from SysInternals http://technet.microsoft.com/en-us/sysinternals/bb897437.aspx

     


    Michael Hidalgo Fallas
    Friday, March 26, 2010 7:28 PM
  • Are you using a Server Farm or Network Load Balance, If yes. what are you using for? Usually  happens when the destination port is being  blocked with a firewall.

    Try to use TCP View from SysInternals http://technet.microsoft.com/en-us/sysinternals/bb897437.aspx

     


    Michael Hidalgo Fallas

    Had the same issue, TCP View points me that was a multicast problem. It fills with Sync send and Sync Rvc pendings connections. Thanks
    Monday, June 21, 2010 7:40 AM
  • I wanted to drop a note and say that if you are troubleshooting connection issues with System.Data.SqlClient, enabling System.Net tracing will not log anything, because SqlConnection does not use System.Net. I spent almost a day trying to get this troubleshooting technique to work before I realized what my problem was. I hope I can save someone else the heartache!
    Friday, January 7, 2011 3:57 PM