/// <summary> /// 获取一次Tcp端口快照信息 /// </summary> /// <returns></returns> public unsafe static List <TcpPort> Snapshot() { var portList = new List <TcpPort>(); const int AF_INET = 2; const int TCP_TABLE_OWNER_PID_ALL = 5; var size = 0; TcpTable.GetExtendedTcpTable(null, &size, true, AF_INET, TCP_TABLE_OWNER_PID_ALL, 0); byte *pTable = stackalloc byte[size]; if (TcpTable.GetExtendedTcpTable(pTable, &size, true, AF_INET, TCP_TABLE_OWNER_PID_ALL, 0) == 0) { var rowLength = *(int *)(pTable); var pRow = pTable + Marshal.SizeOf(rowLength); for (int i = 0; i < rowLength; i++) { var row = *(MIB_TCPROW_OWNER_PID *)pRow; var tcpPort = TcpPort.FromTcpRow(row); if (portList.Contains(tcpPort) == false) { portList.Add(tcpPort); } pRow = pRow + Marshal.SizeOf(typeof(MIB_TCPROW_OWNER_PID)); } } return(portList); }