我在 C# 多线程 Windows 服务中发送推送通知时遇到了一些问题。
当我向 APNS 发送大量通知时,某些线程会抛出异常:
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) ...
我有一个包含 16 个线程的线程池,每个线程都打开一个到 Apple 的连接。
这不是超时,因为我已经尝试过:sslStream.WriteTimeout = 60000;
我也尝试过:Client.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.KeepAlive, false);
发送开始时连接没有关闭
编辑:我认为所有数据都已发送,Apple 突然关闭了连接。
您知道此异常的来源吗? 怎么解决?
如果需要,我可以给你一些代码。
非常感谢您的回答!
最佳答案
异常“现有连接被远程主机强行关闭”通常意味着您打开连接的另一台主机决定不再与您交谈。为此,它会发送一个设置了 RST 的 FIN,以从其一端关闭连接。
由于当您发送大量通知时会发生这种情况,服务是否可能故意限制您以防止过载?
关于c# - 发送大量数据时“连接被远程主机强行关闭”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5326634/