cxi MAC IP TCPリストを取得する
15905 ワード
baiduからの転載:http://hi.baidu.com/jackeyrain/item/ff94efcfd5cf3a3099b498e9
namespace Public
{
public class NativeFunc
{
[StructLayout(LayoutKind.Sequential)]
public class MIB_TCPROW
{
public int dwState;
public int dwLocalAddr;
public int dwLocalPort;
public int dwRemoteAddr;
public int dwRemotePort;
}
[StructLayout(LayoutKind.Sequential)]
public class MIB_TCPTABLE
{
public int dwNumEntries;
public MIB_TCPROW[] table;
}
[DllImport("Iphlpapi.dll")]
static extern int GetTcpTable(IntPtr pTcpTable, ref int pdwSize, bool bOrder);
[DllImport("Iphlpapi.dll")]
static extern int SendARP(Int32 DestIP, Int32 SrcIP, ref Int64 MacAddr, ref Int32 PhyAddrLen);
[DllImport("Ws2_32.dll")]
static extern Int32 inet_addr(string ipaddr);
[DllImport("Ws2_32.dll")]
static extern ushort ntohs(ushort netshort);
//SendArp MAC
public static string GetMacAddress(string macip)
{
StringBuilder strReturn = new StringBuilder();
try
{
Int32 remote = inet_addr(macip);
Int64 macinfo = new Int64();
Int32 length = 6;
SendARP(remote, 0, ref macinfo, ref length);
string temp = System.Convert.ToString(macinfo, 16).PadLeft(12, '0').ToUpper();
int x = 12;
for (int i = 0; i < 6; i++)
{
if (i == 5) { strReturn.Append(temp.Substring(x - 2, 2)); }
else { strReturn.Append(temp.Substring(x - 2, 2) + ":"); }
x -= 2;
}
return strReturn.ToString();
}
catch
{
return string.Empty;
}
}
public static bool IsHostAlive(string strHostIP)
{
string strHostMac = GetMacAddress(strHostIP);
return !string.IsNullOrEmpty(strHostMac);
}
public static MIB_TCPTABLE GetTcpTableInfo()
{
// Tcp
IntPtr hTcpTableData = IntPtr.Zero;
// hTcpTableData
int iBufferSize = 0;
// MIB_TCPTABLE ,
MIB_TCPTABLE tcpTable = new MIB_TCPTABLE();
// List MIB_TCPROW
List<MIB_TCPROW> lstTcpRows = new List<MIB_TCPROW>();
// API ,iBufferSize 0,
// API GetTcpTable ERROR_INSUFFICIENT_BUFFER
//
GetTcpTable(hTcpTableData, ref iBufferSize, false);
//
hTcpTableData = Marshal.AllocHGlobal(iBufferSize);
// MIB_TCPROW
int iTcpRowLen = Marshal.SizeOf(typeof(MIB_TCPROW));
// MIB_TCPTABLE MIB_TCPROW
// -sizeof(int) MIB_TCPTABLE dwNumEntries
int aryTcpRowLength = (int)Math.Ceiling((double)(iBufferSize - sizeof(int)) / iTcpRowLen);
// TcpTable
GetTcpTable(hTcpTableData, ref iBufferSize, false);
// , MIB_TCPTABLE ,
//
for (int i = 0; i < aryTcpRowLength; i++)
{
//hTcpTableData MIB_TCPTABLE ,
// hTcpTableData+4( sizeof(dwNumEntries) ) MIB_TCPROW
IntPtr hTempTableRow = new IntPtr(hTcpTableData.ToInt32() + 4 + i * iTcpRowLen);
MIB_TCPROW tcpRow = new MIB_TCPROW();
tcpRow.dwLocalAddr = 0;
tcpRow.dwLocalPort = 0;
tcpRow.dwRemoteAddr = 0;
tcpRow.dwRemotePort = 0;
tcpRow.dwState = 0;
// 。
Marshal.PtrToStructure(hTempTableRow, tcpRow);
lstTcpRows.Add(tcpRow);
}
tcpTable.dwNumEntries = lstTcpRows.Count;
tcpTable.table = new MIB_TCPROW[lstTcpRows.Count];
lstTcpRows.CopyTo(tcpTable.table);
return tcpTable;
}
public static string GetIpAddress(long ipAddrs)
{
try
{
System.Net.IPAddress ipAddress = new System.Net.IPAddress(ipAddrs);
return ipAddress.ToString();
}
catch { return ipAddrs.ToString(); }
}
public static ushort GetTcpPort(int tcpPort)
{
return ntohs((ushort)tcpPort);
}
public static bool IsPortBusy(int port)
{
MIB_TCPTABLE tcpTableData = GetTcpTableInfo();
return false;
}
}
public class MyApp
{
//
public static void Main()
{
NativeFunc.MIB_TCPTABLE tcpTableData = new NativeFunc.MIB_TCPTABLE();
tcpTableData = NativeFunc.GetTcpTableInfo();
for (int i = 0; i < tcpTableData.dwNumEntries; i++)
{
Console.WriteLine(string.Format("{0}:{1}-->>{2}:{3}",
NativeFunc.GetIpAddress(tcpTableData.table[i].dwLocalAddr),
NativeFunc.GetTcpPort(tcpTableData.table[i].dwLocalPort).ToString(),
NativeFunc.GetIpAddress(tcpTableData.table[i].dwRemoteAddr),
NativeFunc.GetTcpPort(tcpTableData.table[i].dwRemotePort).ToString()));
}
Console.Read();
}
}
}