[:en]Example Code Application[:fr]Exemple d’application de code[:] | VESTEL B2B Support Portal

[:en]Example Code Application[:fr]Exemple d’application de code[:]

[:en]Example Code Application[:fr]Exemple d’application de code[:]

[:en]

This code describes sample posting SETMUTE commands on C# with telnet connection. It consists of two parts: Main Class is the main part of program, it includes codes which execute the program. Minimalistic Telnet Class is the part created for performing telnet operations on C#. One can write their own program using example code application.

Main Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication5
{
    class Program
    {
       static void Main(string[] args)
       {
            string TVIP=192.168.1.134;
            int port=1986;
            MinimalisticTelnet.TelnetConnection tc = new MinimalisticTelnet.TelnetConnection(TVIP, port);
            tc.WriteLine(“SETMUTE”);
            Console.WriteLine(tc.Read());
            Console.ReadLine();
        }
    }
 }

Minimalistic Telnet Class

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Threading;
namespace MinimalisticTelnet
{
    enum Verbs
    {
        WILL = 251,
        WONT = 252,
        DO = 253,
        DONT = 254,
        IAC = 255
    }
    enum Options
    {
        SGA = 3
    }
    class TelnetConnection
    {
        TcpClient tcpSocket;
        //public int modelID;
        int wait = 100; /* Default Sleep intervals in ms */
        public TelnetConnection(string Hostname, int Port)
        {
            tcpSocket = new TcpClient(Hostname, Port);
            Thread.Sleep(300);
        }
        public string Login(string Username, string Password, int LoginTimeOutMs)
        {
            int oldTimeOutMs = wait;
            wait = LoginTimeOutMs;
            string s = Read();
            //if (!s.TrimEnd().EndsWith(“:”))
            //throw new Exception(“Failed to connect : no login prompt”);
            WriteLine(Username);
            //if (!s.TrimEnd().EndsWith(“:”))
            //throw new Exception(“Failed to connect : no password prompt”);
            WriteLine(Password);
            Thread.Sleep(wait);
            s += Read();
            wait = oldTimeOutMs;
            return s;
        }
        public void WriteLine(string cmd)
        {
            Write(cmd + “\n”);
        }
        public void Write(string cmd)
        {
            if (!tcpSocket.Connected) return;
            byte[] buf = System.Text.ASCIIEncoding.ASCII.GetBytes(cmd.Replace(“\0xFF”, “\0xFF\0xFF”));
            tcpSocket.GetStream().Write(buf, 0, buf.Length);
            Thread.Sleep(wait);
        }
        public string Read()
        {
            if (!tcpSocket.Connected) return null;
            StringBuilder sb = new StringBuilder();
            do
            {
                ParseTelnet(sb);
                System.Threading.Thread.Sleep(wait);
            } while (tcpSocket.Available > 0);
            return sb.ToString();
        }
        public bool IsConnected
        {
            get { return tcpSocket.Connected; }
        }
        void ParseTelnet(StringBuilder sb)
        {
            while (tcpSocket.Available > 0)
            {
                int input = tcpSocket.GetStream().ReadByte();
                switch (input)
                {
                    case -1:
                        break;
                    case (int)Verbs.IAC:
                        // interpret as command
                        int inpuDisplayerb = tcpSocket.GetStream().ReadByte();
                        if (inpuDisplayerb == -1) break;
                        switch (inpuDisplayerb)
                        {
                            case (int)Verbs.IAC:
                                //literal IAC = 255 escaped, so append char 255 to string
                                sb.Append(inpuDisplayerb);
                                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(inpuDisplayerb == (int)Verbs.DO ? (byte)Verbs.WILL : (byte)Verbs.DO);
                                else
                                    tcpSocket.GetStream().WriteByte(inpuDisplayerb == (int)Verbs.DO ? (byte)Verbs.WONT : (byte)Verbs.DONT);
                                tcpSocket.GetStream().WriteByte((byte)inputoption);
                                break;
                            default:
                                break;
                        }
                        break;
                    default:
                        sb.Append((char)input);
                        break;
                }
            }
        }
        public void SendCommand(string command)
        {
            Write(command + “\n”);
        }
        public string InvokeCommand(string command)
        {
            Write(command + “\n”);
            if (!tcpSocket.Connected) return null;
            StringBuilder sb = new StringBuilder();
            do
            {
                ParseTelnet(sb);
                System.Threading.Thread.Sleep(wait);
            } 
            while (tcpSocket.Available > 0);
            return sb.ToString();
        }
    }
}
[:fr]

Ce code décrit un exemple de publication de commandes SETMUTE en C# avec une connexion Telnet. Il est composé de deux parties : La classe principale (Main) est la partie principale du programme. Elle comprend les codes qui exécutent le programme. La classe Telnet minimaliste est la partie créée pour effectuer des opérations Telnet en C#. Vous pouvez écrire votre propre programme en utilisant un exemple d’application de code.

Classe Main

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication5
{
    class Program
    {
       static void Main(string[] args)
       {
            string TVIP=192.168.1.134;
            int port=1986;
            MinimalisticTelnet.TelnetConnection tc = new MinimalisticTelnet.TelnetConnection(TVIP, port);
            tc.WriteLine(“SETMUTE”);
            Console.WriteLine(tc.Read());
            Console.ReadLine();
        }
    }
 }

Classe Telnet minimaliste

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Threading;
namespace MinimalisticTelnet
{
    enum Verbs
    {
        WILL = 251,
        WONT = 252,
        DO = 253,
        DONT = 254,
        IAC = 255
    }
    enum Options
    {
        SGA = 3
    }
    class TelnetConnection
    {
        TcpClient tcpSocket;
        //public int modelID;
        int wait = 100; /* Default Sleep intervals in ms */
        public TelnetConnection(string Hostname, int Port)
        {
            tcpSocket = new TcpClient(Hostname, Port);
            Thread.Sleep(300);
        }
        public string Login(string Username, string Password, int LoginTimeOutMs)
        {
            int oldTimeOutMs = wait;
            wait = LoginTimeOutMs;
            string s = Read();
            //if (!s.TrimEnd().EndsWith(“:”))
            //throw new Exception(“Failed to connect : no login prompt”);
            WriteLine(Username);
            //if (!s.TrimEnd().EndsWith(“:”))
            //throw new Exception(“Failed to connect : no password prompt”);
            WriteLine(Password);
            Thread.Sleep(wait);
            s += Read();
            wait = oldTimeOutMs;
            return s;
        }
        public void WriteLine(string cmd)
        {
            Write(cmd + “\n”);
        }
        public void Write(string cmd)
        {
            if (!tcpSocket.Connected) return;
            byte[] buf = System.Text.ASCIIEncoding.ASCII.GetBytes(cmd.Replace(“\0xFF”, “\0xFF\0xFF”));
            tcpSocket.GetStream().Write(buf, 0, buf.Length);
            Thread.Sleep(wait);
        }
        public string Read()
        {
            if (!tcpSocket.Connected) return null;
            StringBuilder sb = new StringBuilder();
            do
            {
                ParseTelnet(sb);
                System.Threading.Thread.Sleep(wait);
            } while (tcpSocket.Available > 0);
            return sb.ToString();
        }
        public bool IsConnected
        {
            get { return tcpSocket.Connected; }
        }
        void ParseTelnet(StringBuilder sb)
        {
            while (tcpSocket.Available > 0)
            {
                int input = tcpSocket.GetStream().ReadByte();
                switch (input)
                {
                    case -1:
                        break;
                    case (int)Verbs.IAC:
                        // interpret as command
                        int inpuDisplayerb = tcpSocket.GetStream().ReadByte();
                        if (inpuDisplayerb == -1) break;
                        switch (inpuDisplayerb)
                        {
                            case (int)Verbs.IAC:
                                //literal IAC = 255 escaped, so append char 255 to string
                                sb.Append(inpuDisplayerb);
                                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(inpuDisplayerb == (int)Verbs.DO ? (byte)Verbs.WILL : (byte)Verbs.DO);
                                else
                                    tcpSocket.GetStream().WriteByte(inpuDisplayerb == (int)Verbs.DO ? (byte)Verbs.WONT : (byte)Verbs.DONT);
                                tcpSocket.GetStream().WriteByte((byte)inputoption);
                                break;
                            default:
                                break;
                        }
                        break;
                    default:
                        sb.Append((char)input);
                        break;
                }
            }
        }
        public void SendCommand(string command)
        {
            Write(command + “\n”);
        }
        public string InvokeCommand(string command)
        {
            Write(command + “\n”);
            if (!tcpSocket.Connected) return null;
            StringBuilder sb = new StringBuilder();
            do
            {
                ParseTelnet(sb);
                System.Threading.Thread.Sleep(wait);
            } 
            while (tcpSocket.Available > 0);
            return sb.ToString();
        }
    }
}
[:]​