'Connection was forcibly closed by the remote host' when sending a lot of data

malinois picture malinois · Mar 16, 2011 · Viewed 9.3k times · Source

I have some troubles with the sending of push notifications in a C# multithread windows service.
When I send a lot of notifications to the APNS, some of threads throws an exception :

Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host.
   at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size)
   at System.Net.Security._SslStream.StartWriting(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security._SslStream.ProcessWrite(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslStream.Write(Byte[] buffer) ...

I have a threadPool of 16 threads, and each thread open a connexion to Apple.
It's not a timeout because i have tried with : sslStream.WriteTimeout = 60000;
I have also tried with: Client.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.KeepAlive, false);

The connection is not closed at the beginning of the sending.

EDIT : I think that all data are sent, and Apple close the connection suddenly.

Have you an idea of the origin of this exception ? How resolve it ?

I can give you some code if it's needed.

Thanks a lot for your answers !

Answer

Frank Boyne picture Frank Boyne · Mar 16, 2011

The exception "An existing connection was forcibly closed by the remote host" typically means that the other host you had the connection open to decided not to talk to you any more. To do this it sends a FIN with RST set to shut down the connection from its end.

Since this happens when you send a lot of notifications, is it possible the service is deliberately throttling you to prevent an overload?