A
A
algotrader20132015-06-22 12:28:58
Computer networks
algotrader2013, 2015-06-22 12:28:58

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

1 answer(s)
X
xmoonlight, 2015-06-22
@xmoonlight

listener.Start();
while(running) {
      TcpClient client = listener.AcceptTcpClient();
      // обработка запроса клиента...............
      client.Close();
}
listener.Stop();

Temporarily move the debugging of the client request processing logic to a separate thread, and start the server itself and leave it alone...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question