Preventing TIME_WAIT using Async .NET API

I have a problem, I developed the Client and Server shell for personal use, but, unfortunately, due to insufficient knowledge in network programming, I have problems with TIME_WAIT while connecting to the client. My client tries to make several connections to the same host for a short period of time, I found out that the main reason for this is that I am trying to reuse the socket and it goes into TIME_WAIT state because I close the connection without graceful shutdowns. I would like to know the correct template for closing a connection using .NET sockets if I use Async APIs intensively, such as ConnectAsync, AcceptAsync, SendAsync, ReceiveAsync, DisconnectAsync (DisconnectAsync - socket reuse)

+2
source share
2 answers

You can use SO_REUSEADDR on the socket to get around this. See Socket.SetSocketOption for more details. This is the ReuseAddress parameter that needs to be set.

By the way, you really don't mean socket reuse? as soon as you receive the error message, you need to close it and open a new one.

+2
source

I found out that it is impossible to prevent TIME_WAIT. Any server or client will have a problem in any way, depending on who first initiates the closure of the connection. If the client that closes the connection will not have TIME_WAIT on the server. If this is the server that closes first, then the client will not have TIME_WAIT. Therefore, the only option that needs to be done is to use SO_REUSEADDR, but in this case it is still impossible to use a reusable address to communicate with a previously disconnected host

+6
source

Source: https://habr.com/ru/post/1257351/


All Articles