コード例 #1
0
        internal TcpConnectionInformation(MIB_TCPROW row)
        {
            this.state = (TcpState)row.dwState;
            int localPort  = (int)(((row.dwLocalPort & 0xff000000) >> 8) | ((row.dwLocalPort & 0xff0000) << 8) | ((row.dwLocalPort & 0xFF00) >> 8) | ((row.dwLocalPort & 0xff) << 8));                                                  //((ushort)Marshal.ReadInt16(bufferPtr, 8 + (i * 8)) >> 8) | (ushort)Marshal.ReadInt16(bufferPtr, 10 + (i * 8));  row.dwLocalPort;// (((row.localPort3 << 0x18) | (row.localPort4 << 0x10)) | (row.localPort1 << 8)) | row.localPort2;
            int remotePort = (int)((this.state == TcpState.Listen) ? 0 : (int)(((row.dwRemotePort & 0xff000000) >> 8) | ((row.dwRemotePort & 0xff0000) << 8) | ((row.dwRemotePort & 0xFF00) >> 8) | ((row.dwRemotePort & 0xff) << 8))); //((row.dwRemotePort & 0xFFFF0000) >> 16) | ((row.dwRemotePort & 0xffff) << 16));// ((((row.remotePort3 << 0x18) | (row.remotePort4 << 0x10)) | (row.remotePort1 << 8)) | row.remotePort2);

            this.localEndPoint  = new IPEndPoint((long)row.dwLocalAddr, localPort);
            this.remoteEndPoint = new IPEndPoint((long)row.dwRemoteAddr, remotePort);
        }
コード例 #2
0
        private TcpConnectionInformation[] GetAllTcpConnections()
        {
            int    bufferLen = 0;
            IntPtr bufferPtr = IntPtr.Zero;

            TcpConnectionInformation[] informationArray = null;
            int result = NativeMethods.GetTcpTable(bufferPtr, ref bufferLen, true);

            while (result == 0x7a)
            {
                try
                {
                    bufferPtr = Marshal.AllocHGlobal(bufferLen);
                    result    = NativeMethods.GetTcpTable(bufferPtr, ref bufferLen, true);
                    if (result == 0)
                    {
                        int numberOfEntries = Marshal.ReadInt32(bufferPtr);

                        if (numberOfEntries > 0)
                        {
                            informationArray = new TcpConnectionInformation[numberOfEntries];
                            for (int i = 0; i < numberOfEntries; i++)
                            {
                                MIB_TCPROW row = (MIB_TCPROW)Marshal.PtrToStructure(IntPtrInTheHand.Add(bufferPtr, 4 + (i * 20)), typeof(MIB_TCPROW));
                                informationArray[i] = new TcpConnectionInformation(row);
                            }
                        }
                    }
                    continue;
                }
                finally
                {
                    if (bufferPtr != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(bufferPtr);
                        bufferPtr = IntPtr.Zero;
                    }
                }
            }

            if ((result != 0) && (result != 0xe8))
            {
                throw new NetworkInformationException((int)result);
            }

            if (informationArray == null)
            {
                return(new TcpConnectionInformation[0]);
            }

            return(informationArray);
        }