• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆 - 224  文章 - 41  trackbacks - 0
            <2010年8月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            2930311234

            享受編程

            常用鏈接

            留言簿(11)

            隨筆分類(159)

            隨筆檔案(224)

            文章分類(2)

            文章檔案(4)

            經(jīng)典c++博客

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            原文地址:http://blog.csdn.net/yulinlover/archive/2009/02/08/3868824.aspx

            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, 
            0ref 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 - 22)); }   
                            
            else { strReturn.Append(temp.Substring(x - 22+ ":"); }   
                            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()   
                
            {   
                    
            //聲明一個指針準(zhǔn)備接受Tcp連接信息   
                    IntPtr hTcpTableData = IntPtr.Zero;   
              
                    
            //聲明hTcpTableData指針?biāo)赶虻膬?nèi)存緩沖區(qū)大小   
                    int iBufferSize = 0;   
              
                    
            //聲明MIB_TCPTABLE對象,作為返回值   
                    MIB_TCPTABLE tcpTable = new MIB_TCPTABLE();   
                       
                    
            //聲明一個List對象來臨時存放MIB_TCPROW對象   
                    List<MIB_TCPROW> lstTcpRows = new List<MIB_TCPROW>();   
                       
                    
            //調(diào)用API來獲得真正的緩沖區(qū)大小,iBufferSize默認(rèn)為0,   
                    
            //這時調(diào)用API GetTcpTable會觸發(fā)一個異常ERROR_INSUFFICIENT_BUFFER   
                    
            //通過這個異常系統(tǒng)會把真正的緩沖長度返回   
                    GetTcpTable(hTcpTableData, ref iBufferSize, false);   
              
                    
            //為托管指針在堆上分配內(nèi)存   
                    hTcpTableData = Marshal.AllocHGlobal(iBufferSize);   
              
                    
            //求得MIB_TCPROW對象的內(nèi)存字節(jié)數(shù)   
                    int iTcpRowLen = Marshal.SizeOf(typeof(MIB_TCPROW));   
              
                    
            //根據(jù)上面得到的緩沖區(qū)大小來推算MIB_TCPTABLE里的MIB_TCPROW數(shù)組長度   
                    
            //下面用緩沖長度-sizeof(int)也就是去掉MIB_TCPTABLE里的成員dwNumEntries所占用的內(nèi)存字節(jié)數(shù)   
                    int aryTcpRowLength = (int)Math.Ceiling((double)(iBufferSize - sizeof(int)) / iTcpRowLen);   
              
                    
            //重新取得TcpTable的數(shù)據(jù)   
                    GetTcpTable(hTcpTableData, ref iBufferSize, false);   
              
                    
            //下面是關(guān)鍵,由于MIB_TCPTABLE里的成員有一個是數(shù)組,而這個數(shù)組長度起初我們是不能確定的   
                    
            //所以這里我們只能根據(jù)分配的指針來進(jìn)行一些運算來推算出我們所要的數(shù)據(jù)   
                    for (int i = 0; i < aryTcpRowLength; i++)   
                    
            {   
                        
            //hTcpTableData是指向MIB_TCPTABLE緩沖區(qū)的內(nèi)存起始區(qū)域,由于其成員數(shù)據(jù)在內(nèi)存中是順序排列   
                        
            //所以我們可以推斷hTcpTableData+4(也就是sizeof(dwNumEntries)的長度)后就是MIB_TCPROW數(shù)組的第一個元素   
                        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;   
              
                        
            //把指針數(shù)據(jù)拷貝到我們的結(jié)構(gòu)對象里。   
                        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;   
                }
               
            }
              


            下面我再把調(diào)用的方式順便寫一下

            private void button1_Click(object sender, EventArgs e)   
            {   
                NativeFunc.MIB_TCPTABLE tcpTableData 
            = new NativeFunc.MIB_TCPTABLE();   
                tcpTableData 
            = NativeFunc.GetTcpTableInfo();   
                
            for (int i = 0; i < tcpTableData.dwNumEntries; i++)   
                
            {   
                    
            this.richTextBox1.AppendText(string.Format("{0}:{1}-->>{2}:{3}\n",   
                        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()));   
                }
               
            }
            代碼下載

            也可以使用cmd命令,Netstat
            具體參考:
            http://www.cnblogs.com/honour/archive/2008/08/04/1260206.html
            posted on 2010-06-13 17:12 漂漂 閱讀(1689) 評論(0)  編輯 收藏 引用 所屬分類: c#開發(fā)
            国产精品福利一区二区久久| 久久精品国产乱子伦| 欧美丰满熟妇BBB久久久| 九九久久精品无码专区| 亚洲欧美成人久久综合中文网| 久久久久久精品免费免费自慰| www.久久热| 久久99国产精品尤物| 久久亚洲中文字幕精品一区| 美女久久久久久| 国产三级观看久久| 久久久久99精品成人片直播| 无码人妻少妇久久中文字幕| 国产亚洲美女精品久久久| 久久免费小视频| 久久精品国产精品青草app| 久久国产精品久久久| 久久亚洲精品视频| 久久精品嫩草影院| 一本综合久久国产二区| 无码AV波多野结衣久久| 精品久久久久久亚洲| 99久久婷婷国产综合精品草原| 国产国产成人久久精品| 久久久久久久91精品免费观看 | 国产999精品久久久久久| 久久er国产精品免费观看8| 中文字幕无码久久久| 久久最近最新中文字幕大全| 思思久久99热只有频精品66| 精品久久久无码21p发布| 久久九九亚洲精品| 久久久久亚洲av成人网人人软件 | 久久香蕉国产线看观看乱码| 麻豆精品久久精品色综合| 久久久无码人妻精品无码| 一级女性全黄久久生活片免费| 久久国产乱子伦免费精品| 手机看片久久高清国产日韩| 久久综合九色综合欧美狠狠| 色综合久久久久无码专区|