Renci.SshNet.ForwardedPortDynamic.ReadString C# (CSharp) Method

ReadString() private static method

Reads a null terminated string from a socket.
private static ReadString ( Socket socket, System.TimeSpan timeout ) : string
socket Socket The to read from.
timeout System.TimeSpan The timeout to apply to individual reads.
return string
        private static string ReadString(Socket socket, TimeSpan timeout)
        {
            var text = new StringBuilder();
            var buffer = new byte[1];
            while (true)
            {
                if (SocketAbstraction.Read(socket, buffer, 0, 1, timeout) == 0)
                {
                    // SOCKS client closed connection
                    return null;
                }

                var byteRead = buffer[0];
                if (byteRead == 0)
                {
                    // end of the string
                    break;
                }

                var c = (char) byteRead;
                text.Append(c);
            }
            return text.ToString();
        }
    }