0

I wrote an TCP application and I have some problem with it.

When I run the TCP program and listen on port then the program dead, but the port is still listen.

I want to restart it. It shows "System.Net.Sockets.SocektException: Only one usage of each socket is normally permitted".

So it check the "TCPView", I found the process of the listening port is , so I tried to close it. Sadly fail!

enter image description here

TCP View - non-existent enter image description here

Try to kill process by cmd - fail enter image description here

So I want to know how to use c# to close the specific port.

Bing Feng
  • 71
  • 2
  • 11
  • Please post the code. – Michael Puckett II Apr 27 '18 at 05:03
  • As Michael says, post your code. Anyway, it isn't a non-existent process. It's a process with no window. It sounds like your code is keeping your application alive when you exit it. As a solution, aggressively killing a process using a port seems to be like amputating a limb because of a minor cut: overkill and not dealing with the real problem. – ProgrammingLlama Apr 27 '18 at 05:04
  • I don't know what code should I post – Bing Feng Apr 27 '18 at 05:13
  • Possible duplicate of [PID exists in netstat but does not exist in task manager](https://stackoverflow.com/questions/15216881/pid-exists-in-netstat-but-does-not-exist-in-task-manager) – Nyerguds Apr 27 '18 at 05:27
  • Experts Exchange had someone saying "these processes are inactive and marked for termination but not already terminated." Not sure how relevant / correct that is though. – Nyerguds Apr 27 '18 at 05:28
  • I have seen that post it doesn't work too. – Bing Feng Apr 27 '18 at 05:29
  • There's [another one](https://superuser.com/questions/191416/pid-number-in-task-manager-cant-be-found) linked from there. Could that one help? – Nyerguds Apr 27 '18 at 05:30
  • This post want to find out the pid number. I can use cmd to find pid number but I can not kill it. – Bing Feng Apr 27 '18 at 05:40

1 Answers1

0

This sparks a memory from back in the day i was all into tcp servers and clients. I thought the OS took time to clean up ports from crashed or aggressively killed applications.

If this is not the case, it might be because your application started another process, i.e a child process.

Anyway, i think you should start from first principles here, if you are in control of the process you are trying to kill, its better to exit this application gracefully, and not trying to just kill it. Also if this application has a main window you could try calling

p.CloseMainWindow();
p.WaitForExit();

Also make sure it hasn't started any child processes, as you "may" have to close them as well.

If you are in control of this application, try binding with ReuseAddress however this is just trying to make up for bad design anyway

Allows the socket to be bound to an address that is already in use.

also you may want to take a look at assicoated socket options

Socket.SetSocketOption Method (SocketOptionLevel, SocketOptionName, Int32)

SocketOptionName Enumeration

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • I want to kill the pid by another program. If the TCP application closed, I can restart application automatically, so I need to make sure that the program can execute successfully. – Bing Feng Apr 27 '18 at 05:46