C#Telnet ANSI转换

时间:2016-10-05 13:42:14

标签: c# converter telnet ansi

我有一个TelnetConnection到服务器,我在我的屏幕上显示日志。 看起来总是如此:

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

我认为这是ANSI。我怎么转换它? 提前谢谢!

Homeros

我的代码:

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;
        }
    }
}

0 个答案:

没有答案