C# Telnet ANSI 转换

问题描述 投票:0回答:0

我有到服务器的 Telnet 连接,我在屏幕上显示日志。 它看起来总是这样:

Test1   [ [91mERROR[0m ]
Test2   [  [92mOK[0m  ]
Test3   [  [92mOK[0m  ]
Overall [[91mERROR[0m ]

我认为那是 ANSI。我怎样才能转换它? 提前谢谢你!

荷马

我的代码:

public string Read()
{
    if (!tcpSocket.Connected) return null;
    StringBuilder sb = new StringBuilder();
    do
    {
        System.Windows.Forms.Application.DoEvents();
        ParseTelnet(sb);
        System.Threading.Thread.Sleep(TimeOutMs);
    } while (tcpSocket.Available > 0);
    sb.ToString();
    return sb.ToString();
}

public bool IsConnected
{
    get { return tcpSocket.Connected; }
}

void ParseTelnet(StringBuilder sb)
{
    while (tcpSocket.Available > 0)
    {
        System.Windows.Forms.Application.DoEvents();
        int input = tcpSocket.GetStream().ReadByte();
        switch (input)
        {
            case -1:
                break;
            case (int)Verbs.IAC:
                // interpret as command
                int inputverb = tcpSocket.GetStream().ReadByte();
                if (inputverb == -1) break;
                switch (inputverb)
                {
                    case (int)Verbs.IAC:
                        //literal IAC = 255 escaped, so append char 255 to string
                        sb.Append(inputverb);
                        break;
                    case (int)Verbs.DO:
                    case (int)Verbs.DONT:
                    case (int)Verbs.WILL:
                    case (int)Verbs.WONT:
                        // reply to all commands with "WONT", unless it is SGA (suppres go ahead)
                        int inputoption = tcpSocket.GetStream().ReadByte();
                        if (inputoption == -1) break;
                        tcpSocket.GetStream().WriteByte((byte)Verbs.IAC);
                        if (inputoption == (int)Options.SGA)
                            tcpSocket.GetStream().WriteByte(inputverb == (int)Verbs.DO ? (byte)Verbs.WILL : (byte)Verbs.DO);
                        else
                            tcpSocket.GetStream().WriteByte(inputverb == (int)Verbs.DO ? (byte)Verbs.WONT : (byte)Verbs.DONT);
                        tcpSocket.GetStream().WriteByte((byte)inputoption);
                        break;
                    default:
                        break;
                }
                break;
            default:
                sb.Append((char)input);
                break;
        }
    }
}
c# converters telnet ansi-escape
© www.soinside.com 2019 - 2024. All rights reserved.