You do a bad thing: you create threads in loop. For networking, you need either one thread (on client side) or two threads on server side: one is accepting new connections (adding info on remote thread to some container), another thread execute data exchange protocol: read/write from/to network stream. Also, don't use
— socket is itself a thread synchronization primitive. You need to create thread(s) from the very beginning and abort them when you close the application.
Also, if you use
, you must process
The number of threads should be fixed.
See my answer to a similar question where I describe the skeleton of right design:
Multple clients from same port Number
You never want to "remove application from Processes
You problem is the application which is not finished due to pending thread(s). One solution could be using thread pool
— the unfinished threads will not prevent your process from closing. This won't be a resolution of the code problem, which is in the logic of your thread life-time control. I don't want to look for your bug, because you design looks wrong. You need to address the design using my directions above.