Answer the question
In order to leave comments, you need to log in
How to correctly handle TcpListener closing when the application stops?
Good afternoon
I have an application that constantly accepts clients on TcpListener'e. When debugging, I often stop it by pressing the stop button in Visual Studio. This kills all application threads, but the port remains occupied and netstat -aon shows that it is occupied by a non-existent process (there is no process with this Id in the task manager). You have to restart your computer to free the port. Tell me how to properly handle such an application stop.
Answer the question
In order to leave comments, you need to log in
listener.Start();
while(running) {
TcpClient client = listener.AcceptTcpClient();
// обработка запроса клиента...............
client.Close();
}
listener.Stop();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question