Preventing TIME_WAIT using .NET #39;Async#39; API(使用 .NET Async API 防止 TIME_WAIT)
问题描述
我有一个问题,我已经开发了一个客户端和服务器包装器供我个人使用,但不幸的是由于网络编程知识不足,我在客户端连接期间遇到了 TIME_WAIT 问题.我的客户端现在尝试在短时间内与同一主机建立多个连接,我发现其主要原因是因为我试图重用套接字并且它进入 TIME_WAIT 状态,因为我正在关闭没有正常关闭的连接.我想知道使用 .NET 套接字关闭连接的正确模式,以防我大量使用异步"API,即 ConnectAsync、AcceptAsync、SendAsync、ReceiveAsync、DisconnectAsync(DisconnectAsync - 重用套接字)等函数
I have a problem, I've developed a Client and Server wrapper for my personal use, but unfortunately due to insufficient knowledge in network programming, I have TIME_WAIT problems during connect on the client. My client tries to make multiple connections to the same host within short period of time now, I have found out that the main reason for that is because I'm trying to reuse the socket and it goes to TIME_WAIT state because I'm closing the connection without graceful shutdown. I would like to know the correct pattern to close connection using .NET sockets in case I'm using 'Async' APIs intensively i.e. functions like ConnectAsync, AcceptAsync, SendAsync, ReceiveAsync, DisconnectAsync (DisconnectAsync - reuses socket)
推荐答案
您可以在套接字上使用 SO_REUSEADDR 来解决这个问题.有关详细信息,请参阅 Socket.SetSocketOption,它是 ReuseAddress
需要设置的选项.
You can use SO_REUSEADDR on the socket to get around this. See Socket.SetSocketOption for details, it's the ReuseAddress
option you need to set.
顺便说一句,您不是真的要重用套接字吗?一旦出现错误,您必须将其关闭并打开一个新错误.
By the way you don't really mean reuse the socket do you? once you get an error, you have to close it and open a new one.
这篇关于使用 .NET 'Async' API 防止 TIME_WAIT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 .NET 'Async' API 防止 TIME_WAIT


- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01