“连接被远程主机强制关闭”当发送大量数据时

发布于 2024-10-22 00:21:32 字数 1003 浏览 10 评论 0原文

我在 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 突然关闭了连接。

您知道这个异常的起源吗? 怎么解决呢?

如果需要的话我可以给你一些代码。

非常感谢您的回答!

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 !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

遗弃M 2024-10-29 00:21:32

异常“现有连接被远程主机强制关闭”通常意味着您打开连接的另一台主机决定不再与您通信。为此,它发送一个带有 RST 设置的 FIN,以从一端关闭连接。

由于当您发送大量通知时会发生这种情况,因此该服务是否可能故意限制您以防止过载?

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?

ゃ人海孤独症 2024-10-29 00:21:32

从 APNS-Sharp 我发现用户在传入错误的设备令牌时会看到此错误 - http://code.google.com/p/apns-sharp/issues/detail?id=35

如果这不是答案,那么您能否确认您的代码没有当你打开较少的线程时不起作用?或者当您发送少量数据时它是否有效?

顺便说一句,真的有必要同时打开 16 个连接吗?这看起来相当高。

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.

舞袖。长 2024-10-29 00:21:32

这与推送通知无关,但我最近在多线程应用程序中遇到了这个问题,因为我有一个在两个线程之间共享的套接字(一个用于发送数据,一个用于接收)。当接收线程在 socket.ReceiveFrom() 中阻塞时,发送线程正尝试在同一个套接字上发送数据。这导致我的应用程序中出现“连接被远程主机强制关闭”错误。这可能是您应该研究的内容。

我最终通过添加一些 WaitHandles 来使套接字连接线程安全来解决我的问题。

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.

又爬满兰若 2024-10-29 00:21:32

可能是您有太多打开的连接,而苹果的服务器出于某种原因关闭了它们。
尝试查看在连接数较少的情况下问题是否仍然存在。

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.

執念 2024-10-29 00:21:32

没有解决方案,但我认为苹果在没有更多数据发送时关闭连接以便重用套接字。

为了避免错误,我现在使用增强的通知。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文