C # Check which process occupies the specified port

Source: Internet
Author: User

1. Check whether port 80 in the current system is being used by other processes;

2. If port 80 is in use, find the process and force it to end.

3. Use C # For language implementation.

 

When someone asks me a question, I usually google it to help them find a suitable one. This is no exception! But unfortunately, I did not find a solution. So I wrote this article, hoping to give some help to my friends, and hope to communicate with more friends!

 

To solve the first problem, you can directly request the system to bind port 80 in the program. If the binding fails (an exception is thrown), Port 80 is occupied, this is also the most common method for writing network programs.

 

If you want to check which process is using port 80, you may have to worry about it. A network engineer should be very familiar with a command: netstat-ano. When running this command on the console (CMD), all the ports currently occupied can be listed, we may also use the Command provided in the system to analyze the running result!

 

A simple solution is as follows:

1. Start a new process in the program. The execution file of the process is CMD. EXE.

2. Pass the command line parameter netstat-ano to the process.

3. Obtain the result returned by the command and analyze it to find the process ID (PID) with port 80)

4. Identify the process based on the PID and perform any processing on the process.

 

C # implementation code (since the code here needs to be very concise, it does not take into account any error handling issues. If a friend directly uses the project, the author shall not be liable for any problems caused ):

 

Code
 Static void Main (string [] args ){
Process pro = new Process ();

// Set command lines and Parameters
Pro. StartInfo. FileName = "cmd.exe ";
Pro. StartInfo. UseShellExecute = false;
Pro. StartInfo. RedirectStandardInput = true;
Pro. StartInfo. RedirectStandardOutput = true;
Pro. StartInfo. RedirectStandardError = true;
Pro. StartInfo. CreateNoWindow = true;
// Start CMD
Pro. Start ();
// Run the Port Check command
Pro. StandardInput. WriteLine ("netstat-ano ");
Pro. StandardInput. WriteLine ("exit ");

// Obtain the result
Regex reg = new Regex ("\ s +", RegexOptions. Compiled );
String line = null;
While (line = pro. StandardOutput. ReadLine ())! = Null ){
Line = line. Trim ();
If (line. StartsWith ("TCP", StringComparison. OrdinalIgnoreCase )){
Line = reg. Replace (line ,",");

String [] arr = line. Split (,);
If (arr [1]. EndsWith (": 80 ")){
Console. WriteLine ("port 80 process ID: {0}", arr [4]);

Int pid = Int32.Parse (arr [4]);
Process pro80 = Process. getprocpolicyid (pid );
//
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.