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)
Share
You can use SO_REUSEADDR on the socket to get around this. See Socket.SetSocketOption for details, it’s the
ReuseAddressoption 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.