使用.NET“异步”API preventing TIME_WAITAPI、NET、TIME_WAIT、preventing

2023-09-06 07:38:07 作者:o^┢┦apΡy

我有一个问题,我已经开发了客户端和服务器的包装我私人使用的,但遗憾的是由于网络编程知识不足,我必须在连接客户端上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.