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 );
//