public void GetTcpConnections() { byte[] buffer = new byte[20000]; // Start with 20.000 bytes left for information about tcp table int pdwSize = 20000; int res = IPHlpAPI32Wrapper.GetTcpTable(buffer, out pdwSize, true); if (res != Utils.NO_ERROR) { buffer = new byte[pdwSize]; res = IPHlpAPI32Wrapper.GetTcpTable(buffer, out pdwSize, true); if (res != 0) { return; // Error. You should handle it } } TcpConnections = new IpHlpApidotnet.MIB_TCPTABLE(); int nOffset = 0; // number of entry in the TcpConnections.dwNumEntries = Convert.ToInt32(buffer[nOffset]); nOffset += 4; TcpConnections.table = new MIB_TCPROW[TcpConnections.dwNumEntries]; for (int i = 0; i < TcpConnections.dwNumEntries; i++) { // state int st = Convert.ToInt32(buffer[nOffset]); // state in string //((MIB_TCPROW)(TcpConnections.table[i])).StrgState = Utils.StateToStr(st); (TcpConnections.table[i]).StrgState = Utils.StateToStr(st); // state by ID //((MIB_TCPROW)(TcpConnections.table[i])).iState = st; (TcpConnections.table[i]).iState = st; nOffset += 4; // local address //((MIB_TCPROW)(TcpConnections.table[i])).Local = Utils.BufferToIPEndPoint(buffer, ref nOffset, false); (TcpConnections.table[i]).Local = Utils.BufferToIPEndPoint(buffer, ref nOffset, false); // remote address //((MIB_TCPROW)(TcpConnections.table[i])).Remote = Utils.BufferToIPEndPoint(buffer, ref nOffset, true); (TcpConnections.table[i]).Remote = Utils.BufferToIPEndPoint(buffer, ref nOffset, true); } }
public void GetTcpConnexions() { byte[] buffer = new byte[20000]; // Start with 20.000 bytes left for information about tcp table int pdwSize = 20000; int res = IPHlpAPI32Wrapper.GetTcpTable(buffer, out pdwSize, true); if (res != NO_ERROR) { buffer = new byte[pdwSize]; res = IPHlpAPI32Wrapper.GetTcpTable(buffer, out pdwSize, true); if (res != 0) return; // Error. You should handle it } TcpConnexion = new IpHlpApidotnet.MIB_TCPTABLE(); int nOffset = 0; // number of entry in the TcpConnexion.dwNumEntries = Convert.ToInt32(buffer[nOffset]); nOffset += 4; TcpConnexion.table = new MIB_TCPROW[TcpConnexion.dwNumEntries]; for (int i = 0; i < TcpConnexion.dwNumEntries; i++) { // state int st = Convert.ToInt32(buffer[nOffset]); // state in string //((MIB_TCPROW) (TcpConnexion.table[i]).StrgState = convert_state(st); // state by ID //((MIB_TCPROW) (TcpConnexion.table[i]).iState = st; nOffset += 4; // local address string LocalAdrr = buffer[nOffset].ToString() + "." + buffer[nOffset + 1].ToString() + "." + buffer[nOffset + 2].ToString() + "." + buffer[nOffset + 3].ToString(); nOffset += 4; //local port in decimal int LocalPort = (((int)buffer[nOffset]) << 8) + (((int)buffer[nOffset + 1])) + (((int)buffer[nOffset + 2]) << 24) + (((int)buffer[nOffset + 3]) << 16); nOffset += 4; // store the remote endpoint //((MIB_TCPROW)( (TcpConnexion.table[i]).Local = new IPEndPoint(IPAddress.Parse(LocalAdrr), LocalPort); // remote address string RemoteAdrr = buffer[nOffset].ToString() + "." + buffer[nOffset + 1].ToString() + "." + buffer[nOffset + 2].ToString() + "." + buffer[nOffset + 3].ToString(); nOffset += 4; // if the remote address = 0 (0.0.0.0) the remote port is always 0 // else get the remote port in decimal int RemotePort; // if (RemoteAdrr == "0.0.0.0") { RemotePort = 0; } else { RemotePort = (((int)buffer[nOffset]) << 8) + (((int)buffer[nOffset + 1])) + (((int)buffer[nOffset + 2]) << 24) + (((int)buffer[nOffset + 3]) << 16); } nOffset += 4; //((MIB_TCPROW) (TcpConnexion.table[i]).Remote = new IPEndPoint(IPAddress.Parse(RemoteAdrr), RemotePort); } }
public void GetTcpConnexions() { byte[] buffer = new byte[20000]; // Start with 20.000 bytes left for information about tcp table int pdwSize = 20000; int res = IPHlpAPI32Wrapper.GetTcpTable(buffer, out pdwSize, true); if (res != NO_ERROR) { buffer = new byte[pdwSize]; res = IPHlpAPI32Wrapper.GetTcpTable(buffer, out pdwSize, true); if (res != 0) { return; // Error. You should handle it } } TcpConnexion = new IpHlpApidotnet.MIB_TCPTABLE(); int nOffset = 0; // number of entry in the TcpConnexion.dwNumEntries = Convert.ToInt32(buffer[nOffset]); nOffset += 4; TcpConnexion.table = new MIB_TCPROW[TcpConnexion.dwNumEntries]; for (int i = 0; i < TcpConnexion.dwNumEntries; i++) { // state int st = Convert.ToInt32(buffer[nOffset]); // state in string ((MIB_TCPROW)(TcpConnexion.table[i])).StrgState = convert_state(st); // state by ID ((MIB_TCPROW)(TcpConnexion.table[i])).iState = st; nOffset += 4; // local address string LocalAdrr = buffer[nOffset].ToString() + "." + buffer[nOffset + 1].ToString() + "." + buffer[nOffset + 2].ToString() + "." + buffer[nOffset + 3].ToString(); nOffset += 4; //local port in decimal int LocalPort = (((int)buffer[nOffset]) << 8) + (((int)buffer[nOffset + 1])) + (((int)buffer[nOffset + 2]) << 24) + (((int)buffer[nOffset + 3]) << 16); nOffset += 4; // store the remote endpoint ((MIB_TCPROW)(TcpConnexion.table[i])).Local = new IPEndPoint(IPAddress.Parse(LocalAdrr), LocalPort); // remote address string RemoteAdrr = buffer[nOffset].ToString() + "." + buffer[nOffset + 1].ToString() + "." + buffer[nOffset + 2].ToString() + "." + buffer[nOffset + 3].ToString(); nOffset += 4; // if the remote address = 0 (0.0.0.0) the remote port is always 0 // else get the remote port in decimal int RemotePort; // if (RemoteAdrr == "0.0.0.0") { RemotePort = 0; } else { RemotePort = (((int)buffer[nOffset]) << 8) + (((int)buffer[nOffset + 1])) + (((int)buffer[nOffset + 2]) << 24) + (((int)buffer[nOffset + 3]) << 16); } nOffset += 4; ((MIB_TCPROW)(TcpConnexion.table[i])).Remote = new IPEndPoint(IPAddress.Parse(RemoteAdrr), RemotePort); } }