How to close TCP and UDP ports via windows command line

后端 未结 17 1599
忘掉有多难
忘掉有多难 2020-12-07 06:43

Does somebody knows how to close a TCP or UDP socket for a single connection via windows command line?

Googling about this, I saw some people asking the same thing.

相关标签:
17条回答
  • 2020-12-07 07:15

    Yes, this is possible. You don't have to be the current process owning the socket to close it. Consider for a moment that the remote machine, the network card, the network cable, and your OS can all cause the socket to close.

    Consider also that Fiddler and Desktop VPN software can insert themselves into the network stack and show you all your traffic or reroute all your traffic.

    So all you really need is either for Windows to provide an API that allows this directly, or for someone to have written a program that operates somewhat like a VPN or Fiddler and gives you a way to close sockets that pass through it.

    There is at least one program (CurrPorts) that does exactly this and I used it today for the purpose of closing specific sockets on a process that was started before CurrPorts was started. To do this you must run it as administrator, of course.

    Note that it is probably not easily possible to cause a program to not listen on a port (well, it is possible but that capability is referred to as a firewall...), but I don't think that was being asked here. I believe the question is "how do I selectively close one active connection (socket) to the port my program is listening on?". The wording of the question is a bit off because a port number for the undesired inbound client connection is given and it was referred to as "port" but it's pretty clear that it was a reference to that one socket and not the listening port.

    0 讨论(0)
  • 2020-12-07 07:15

    For instance you want to free the port 8080 Then, follow these commands.

     netstat -ano
     taskkill /f /im [PID of the port 8080 got from previous command]
    

    Done!

    0 讨论(0)
  • 2020-12-07 07:17

    you can use program like tcpview from sysinternal. I guess it can help you a lot on both monitoring and killing unwanted connection.

    0 讨论(0)
  • 2020-12-07 07:19

    Yes there is possible to close TCP or UDP port there is a command in DOS

    TASKKILL /f /pid 1234 
    

    I hope this will work for You

    0 讨论(0)
  • 2020-12-07 07:20
    1. open cmd

      • type in netstat -a -n -o

      • find TCP [the IP address]:[port number] .... #[target_PID]# (ditto for UDP)

      • (Btw, kill [target_PID] didn't work for me)

    2. CTRL+ALT+DELETE and choose "start task manager"

      • Click on "Processes" tab

      • Enable "PID" column by going to: View > Select Columns > Check the box for PID

      • Find the PID of interest and "END PROCESS"

    3. Now you can rerun the server on [the IP address]:[port number] without a problem

    0 讨论(0)
  • 2020-12-07 07:20

    wkillcx is a reliable windows command line tool for killing tcp connections from the command line that hasn't been mentioned. It does have issues with servers with large number of connections sometimes though. I sometimes use tcpview for interactive kills but wkillcx can be used in scripts.

    0 讨论(0)
提交回复
热议问题