1、在Windows下用CMD netstat命令可以获得当前进程监听端口号的信息,如netstat -ano可以看到IP、port、状态和监听的PID。
那么可以执行CMD这个进程得到监听的端口号信息,C#代码如下:
//进程id int pid = ProcInfo.ProcessID; //存放进程使用的端口号链表 List<int> ports = new List<int>(); Process pro = new Process(); 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; pro.Start(); pro.StandardInput.WriteLine("netstat -ano"); pro.StandardInput.WriteLine("exit"); Regex reg = new Regex("\\s+", RegexOptions.Compiled); string line = null; ports.Clear(); 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[4] == pid.ToString()) { string soc = arr[1]; int pos = soc.LastIndexOf(':'); int pot = int.Parse(soc.Substring(pos + 1)); ports.Add(pot); } } else if (line.StartsWith("UDP", StringComparison.OrdinalIgnoreCase)) { line = reg.Replace(line, ","); string[] arr = line.Split(','); if (arr[3] == pid.ToString()) { string soc = arr[1]; int pos = soc.LastIndexOf(':'); int pot = int.Parse(soc.Substring(pos + 1)); ports.Add(pot); } } } pro.Close();
2、如果不执行CMD进程,如何获得?可以参考这篇文章http://www.cnblogs.com/BoyXiao/archive/2012/02/20/2359273.html
文章介绍了使用Windows API获得进程和端口的映射关系:
(1)根据进程 ID 获得该进程所打开的所有的 TCP 和 UDP 端口。
(2)根据端口号来获得打开该端口的进程。
C语言代码如下:
View Code
如果要在.Net平台下使用,将其编译成DLL,使用PInvoke得到DLL导出函数就可以了。
C#测试代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; namespace ProcessorPortDllTest { public enum TcpOrUdp { TcpType, UdpType }; public class ProcessPortHelper { [DllImport("ProcessorPort.dll",CallingConvention = CallingConvention.StdCall)] public extern static uint GetProcessIdByPort(TcpOrUdp type, uint dwPort); [DllImport("ProcessorPort.dll",CallingConvention = CallingConvention.StdCall)] public extern static uint GetAllPortByProcessId(TcpOrUdp type, uint dwProcessId, uint[] dwAllPort, uint dwMaxLen); } class Program { static void Main(string[] args) { uint port = 1025; uint processorId = ProcessPortHelper.GetProcessIdByPort(TcpOrUdp.TcpType, port); Console.WriteLine("Port {0} is using by processor {1}",port,processorId); uint processorId1 = 1072; uint[] TcpPorts = new uint[100]; uint count = ProcessPortHelper.GetAllPortByProcessId(TcpOrUdp.TcpType, processorId1, TcpPorts, (uint)TcpPorts.Length); Console.WriteLine("Processor {0} is using TCP port: ", processorId1); for (uint i = 0; i < count; ++i) { Console.WriteLine(TcpPorts[i]); } uint[] UdpPorts = new uint[100]; uint count1 = ProcessPortHelper.GetAllPortByProcessId(TcpOrUdp.UdpType, processorId1, UdpPorts, (uint)UdpPorts.Length); Console.WriteLine("Processor {0} is using UDP port: ", processorId1); for (uint i = 0; i < count1; ++i) { Console.WriteLine(UdpPorts[i]); }
Console.ReadKey(); } } }
获得进程监听TCP/UDP端口号的DLL:ProcessorPort.rar
本文转自阿凡卢博客园博客,原文链接:http://www.cnblogs.com/luxiaoxun/p/3404176.html,如需转载请自行联系原作者