/// <summary> /// Retrieves the current table of tcp connections. /// </summary> /// <returns> /// An array of tcp connection entries representing the /// tcp table. /// </returns> public static TcpConnection[] GetCurrentTcpConnections() { const int NO_ERROR = 0; // start with a buffer size of 20000 bytes int size = 20000; byte[] buffer = new byte[size]; // call the function once to get the buffer size if (GetTcpTable (buffer, out size, true) != NO_ERROR) { return null; } // allocate the correct number of bytes for the buffer buffer = new byte[size]; // call it again with the correct buffer size if (GetTcpTable (buffer, out size, true) != NO_ERROR) { return null; } // copy out the number of rows int rows = BitConverter.ToInt32 (buffer, 0); // allocate enough space in the table TcpConnection[] connection = new TcpConnection [rows]; #region Read Table // loop through each row for (int i = 0; i < rows; i++) { connection[i] = new TcpConnection(); // copy out the state connection[i].State = (TcpState)BitConverter.ToInt32 (buffer, (i * 20) + 4); // copy out the local address IPAddress localAddress = IPAddress.Parse (buffer[(i * 20) + 8].ToString() + "." + buffer[(i * 20) + 9].ToString() + "." + buffer[(i * 20) + 10].ToString() + "." + buffer[(i * 20) + 11].ToString()); // copy out the local port int localPort = (((int) buffer[(i * 20) + 12]) <<8) + (((int) buffer[(i * 20) + 13])) + (((int) buffer[(i * 20) + 14]) <<24) + (((int) buffer[(i * 20) + 15]) <<16); // store the local end point in the array connection[i].LocalEndPoint = new IPEndPoint (localAddress, localPort); // copy out the remote address IPAddress remoteAddress = IPAddress.Parse (buffer[(i * 20) + 16].ToString() + "." + buffer[(i * 20) + 17].ToString() + "." + buffer[(i * 20) + 18].ToString() + "." + buffer[(i * 20) + 19].ToString()); // copy out the remote port int remotePort = (((int) buffer[(i * 20) + 20]) <<8) + (((int) buffer[(i * 20) + 21])) + (((int) buffer[(i * 20) + 22]) <<24) + (((int) buffer[(i * 20) + 23]) <<16); // store the remote end point in the array connection[i].RemoteEndPoint = new IPEndPoint (remoteAddress, remotePort); } #endregion return connection; }
/// <summary> /// Retrieves the current table of tcp connections. /// </summary> /// <returns> /// An array of tcp connection entries representing the /// tcp table. /// </returns> public static TcpConnection[] GetCurrentTcpConnections() { const int NO_ERROR = 0; // start with a buffer size of 20000 bytes int size = 20000; byte[] buffer = new byte[size]; // call the function once to get the buffer size if (GetTcpTable(buffer, out size, true) != NO_ERROR) { return(null); } // allocate the correct number of bytes for the buffer buffer = new byte[size]; // call it again with the correct buffer size if (GetTcpTable(buffer, out size, true) != NO_ERROR) { return(null); } // copy out the number of rows int rows = BitConverter.ToInt32(buffer, 0); // allocate enough space in the table TcpConnection[] connection = new TcpConnection [rows]; #region Read Table // loop through each row for (int i = 0; i < rows; i++) { connection[i] = new TcpConnection(); // copy out the state connection[i].State = (TcpState)BitConverter.ToInt32(buffer, (i * 20) + 4); // copy out the local address IPAddress localAddress = IPAddress.Parse(buffer[(i * 20) + 8].ToString() + "." + buffer[(i * 20) + 9].ToString() + "." + buffer[(i * 20) + 10].ToString() + "." + buffer[(i * 20) + 11].ToString()); // copy out the local port int localPort = (((int)buffer[(i * 20) + 12]) << 8) + (((int)buffer[(i * 20) + 13])) + (((int)buffer[(i * 20) + 14]) << 24) + (((int)buffer[(i * 20) + 15]) << 16); // store the local end point in the array connection[i].LocalEndPoint = new IPEndPoint(localAddress, localPort); // copy out the remote address IPAddress remoteAddress = IPAddress.Parse(buffer[(i * 20) + 16].ToString() + "." + buffer[(i * 20) + 17].ToString() + "." + buffer[(i * 20) + 18].ToString() + "." + buffer[(i * 20) + 19].ToString()); // copy out the remote port int remotePort = (((int)buffer[(i * 20) + 20]) << 8) + (((int)buffer[(i * 20) + 21])) + (((int)buffer[(i * 20) + 22]) << 24) + (((int)buffer[(i * 20) + 23]) << 16); // store the remote end point in the array connection[i].RemoteEndPoint = new IPEndPoint(remoteAddress, remotePort); } #endregion return(connection); }