Question

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 !

Was it helpful?

Solution 5

No solution, but I think that is Apple who close the connexion in order to reuse the socket, when no more data are sent.

To avoid errors, I use enhanced notifications now.

OTHER TIPS

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?

From APNS-Sharp I've seen that users have seen this error when they've passed in bad device tokens - http://code.google.com/p/apns-sharp/issues/detail?id=35

If that's not the answer, then can you confirm that your code doesn't work when you open less threads? Or does it work when you send smaller quantities of data?

As an aside on this, is it really necessary to open 16 connections at the same time? This seems quite high.

This has nothing to do with push notifications, but I recently experienced this problem in a multi-threaded application, because I had a socket that was shared between two threads (one for sending data and one for receiving). While the receiving thread was blocking in a socket.ReceiveFrom(), the sending thread was attempting to send data on the same socket. This caused the "Connection forcibly closed by the remote host" error in my application. This might be something that you should look in to.

I ended up fixing my problem by adding some a WaitHandles to make the socket connection thread safe.

May be you are having too many open connections and the server at apple for whatever reason closes them. Try to see if the problems persists with less # of connections.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top