none
Unexpected error while downloading files using httpwebrequest RRS feed

  • Question

  • Hi,

    I'm having a multi threaded windows service application. It downloads the files from the server parallely in separate threads.

    It works fine but sometimes I'm getting the following exception while downloading files:

    SendFailure Exception : System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
       at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
       at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
       --- End of inner exception stack trace ---
       at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
       at System.Net.FixedSizeReader.ReadPacket(Byte[] buffer, Int32 offset, Int32 count)
       at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
       at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
       at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
       at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)
       at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)
       at System.Net.TlsStream.CallProcessAuthentication(Object state)
       at System.Threading.ExecutionContext.runTryCode(Object userData)
       at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result)
       at System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size)
       at System.Net.PooledStream.Write(Byte[] buffer, Int32 offset, Int32 size)
       at System.Net.ConnectStream.WriteHeaders(Boolean async)
       --- End of inner exception stack trace ---
       at System.Net.HttpWebRequest.GetResponse()

    Note: I have set ServicePointManager.DefaultConnectionLimit to Int.MaxValue.

    Please suggest.


    Navyatha

    Tuesday, July 9, 2013 10:51 AM

Answers

  • This error is occurring because the remote server is disconnecting.  This is not an uncommon error in client-server code.  You'll have to decided how to handle it (generally by retrying).  Note that I'd wager the server has throttling code in place such that either it allows only a fixed # of connections or (more likely) it allows only a fixed # of connections for a single client. 

    I don't know that you're gaining much by breaking up a download into multiple threads if they are all going to the same remote server.  In most cases the threading is going to overwhelm any slight gains you might have and, irrelevant, you're more likely to be limited by the bandwidth of the local or remote network the more threads you have running.  However if you're downloading a file from multiple remote servers at the same time then you're limited only by the local network bandwidth.

    Michael Taylor
    http://msmvps.com/blogs/p3net

    Tuesday, July 9, 2013 2:12 PM
    Moderator