21
38
1,438
130,970
  현재접속자 : 1 (회원 0)
내 공부 > 프로그래밍
 
작성일 : 16-02-04 15:40
텔넷 구현
 글쓴이 : Minuk Y.
조회 : 2,734  
class Program
    {
        static StreamReader input;
        static StreamWriter output;
        static NetworkStream telnetStream;
        static Boolean isReading= false;
        static Boolean isConnected = false;
        static void Main(string[] args)
        {
            TcpClient telnet = new TcpClient();
            while (!telnet.Connected)
            {
                try {
                    telnet = new TcpClient("192.168.0.200", 23);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            telnetStream = telnet.GetStream();
            output = new StreamWriter(telnetStream);
            input = new StreamReader(telnetStream);
            Thread streamReadingThread = new Thread(new ThreadStart(readStream));
            streamReadingThread.Start();

            if (input != null) isReading = true;
           


            ConsoleKeyInfo keyinfo;
            int Xcnt = 0;

            while (true)
            {
                keyinfo = Console.ReadKey();
                Console.WriteLine(keyinfo.Key + " was pressed");
                if (keyinfo.Key == ConsoleKey.X) Xcnt++;
                else Xcnt = 0;
                if (Xcnt > 5) break;
            }
            isReading = false;
            telnet.Close();
        }
        static String rcvRSSI = "";
        static Char readChar = ' ';
        static Byte[] backSpace = { 8, 8, 8, 8, 8, 8, 8, 8, 8, 8 };
        static Char[] login = "login:".ToCharArray(0, 6);// { 'l', 'o', 'g', 'i', 'n', ':' };
        static Char[] passwd = "Password:".ToCharArray(0, 9);//{ 'p', 'a', 's', 's', 'w', 'o', 'r', 'd', ':' };
        static Char[] IPnDDL = "IPnDDL>".ToCharArray(0, 7);
        static Char[] RSSI = "RSSI : ".ToCharArray(0, 7);
        static void readStream()
        {
            int nlogin = 0;
            int npasswd = 0;
            int nIPnDDL = 0;
            int nRSSI = 0;
            while (isReading)
            {
                if (telnetStream.CanRead)
                {
                    readChar = (Char)telnetStream.ReadByte();
                    //Console.Write(readChar);
                    String sndMsg = "";

                    if (readChar == login[nlogin]) nlogin++;
                    else nlogin = 0;
                    if (nlogin == 6)
                    {
                        sndMsg = "admin\n";
                        nlogin = 0;
                    }

                    if (readChar == passwd[npasswd]) npasswd++;
                    else npasswd = 0;
                    if (npasswd == 9)
                    {
                        sndMsg = "admin\n";
                        npasswd = 0;
                    }

                    if (readChar == IPnDDL[nIPnDDL]) nIPnDDL++;
                    else nIPnDDL = 0;
                    if (nIPnDDL == 7)
                    {
                        Thread.Sleep(10);
                        telnetStream.Write(backSpace, 0, 8);
                        sndMsg = "AT+WRSSI?";
                        nIPnDDL = 0;
                    }
                    if (nRSSI < 7)
                    {
                        if (readChar == RSSI[nRSSI]) nRSSI++;
                        else nRSSI = 0;
                    }
                    else
                    {
                        nRSSI++;
                        rcvRSSI += readChar;
                        if(nRSSI==10)
                        {
                            Double rssi = Double.Parse(rcvRSSI);
                            Double signal = 0.02 * (rssi + 100);
                            if (signal > 1) signal = 1.0;
                            Console.WriteLine(rssi.ToString("0db") + " " +signal.ToString("0%"));
                            nRSSI = 0;
                            rcvRSSI = "";
                        }
                    }
                    Char[] sndChars = sndMsg.ToCharArray();
                    Byte[] sndBytes = new Byte[sndChars.Length];
                    for(int i=0;i< sndChars.Length;i++)
                        sndBytes[i] = (Byte)sndChars[i];

                    telnetStream.Write(sndBytes, 0, sndBytes.Length);
                }
            }
        }
    }

 
   
 

Copyright ⓒ Minuk Y. All rights reserved. . .