System.Net.NetworkInformation.StringParsingHelpers.ParseNumSocketConnections C# (CSharp) Method

ParseNumSocketConnections() static private method

static private ParseNumSocketConnections ( string filePath, string protocolName ) : int
filePath string
protocolName string
return int
        internal static int ParseNumSocketConnections(string filePath, string protocolName)
        {
            if (!File.Exists(filePath))
            {
                throw new PlatformNotSupportedException(SR.net_InformationUnavailableOnPlatform);
            }

            // Parse the number of active connections out of /proc/net/sockstat
            string sockstatFile = File.ReadAllText(filePath);
            int indexOfTcp = sockstatFile.IndexOf(protocolName, StringComparison.Ordinal);
            int endOfTcpLine = sockstatFile.IndexOf(Environment.NewLine, indexOfTcp + 1, StringComparison.Ordinal);
            string tcpLineData = sockstatFile.Substring(indexOfTcp, endOfTcpLine - indexOfTcp);
            StringParser sockstatParser = new StringParser(tcpLineData, ' ');
            sockstatParser.MoveNextOrFail(); // Skip "<name>:"
            sockstatParser.MoveNextOrFail(); // Skip: "inuse"
            return sockstatParser.ParseNextInt32();
        }

Usage Example

Beispiel #1
0
        public LinuxTcpStatistics(bool ipv4)
        {
            _table = StringParsingHelpers.ParseTcpGlobalStatisticsFromSnmpFile(NetworkFiles.SnmpV4StatsFile);

            string sockstatFile = ipv4 ? NetworkFiles.SockstatFile : NetworkFiles.Sockstat6File;
            string protoName    = ipv4 ? "TCP" : "TCP6";

            _currentConnections = StringParsingHelpers.ParseNumSocketConnections(sockstatFile, protoName);
        }
All Usage Examples Of System.Net.NetworkInformation.StringParsingHelpers::ParseNumSocketConnections