简体   繁体   中英

C# kill non-existent process

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!

在此处输入图片说明

TCP View - non-existent 在此处输入图片说明

Try to kill process by cmd - fail 在此处输入图片说明

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

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, ie 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM