如何在命令行的telnet窗口中读取输入和写入输出?

时间:2015-05-26 06:26:05

标签: c# telnet

当我的设备扫描barocode时,我希望从命令行接收输入,并将条形码相关信息提供给telnet窗口上的conmmand行,我们从telnet服务器登录到cmd.exe通过" telnet 192.168.xx 23& #34;在cmd中键入此命令后,登录succsessfull telnet窗口打开并且我的机器连接到设备,现在我必须从该窗口读取条形码字符串并显示与该字符串相关的输出。请告诉我如何做到这一点?

这是我的代码,只需手动输入输入字符串,然后按Enter键输出。

namespace ConsoleApplication2
{
    class Program
    {
    private static System.Timers.Timer aTimer;
    string path = @"C:\Users\Priya\Desktop\Project\barcode.txt";

    SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["SaiNathHospital"].ToString());

    public void getConsoleInput()
    {
        try
        {
            FileInfo fi = new FileInfo(path);

            for (int i = 0; i <= 0; i++)
            {
                Console.WriteLine("");
                using (StreamWriter sw = new StreamWriter(path))
                {
                    sw.WriteLine(Console.ReadLine());
                    sw.Close();
                }
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }

    public void getConsoleInputAtRuntime()
    {

    }

    public void ReadWriteIntoFile()
    {
        try
        {
            string filename = @"C:\Users\Priya\Desktop\Project\Data.txt";
            StringBuilder sb = new StringBuilder();

            StreamReader sr = new StreamReader(path);
            string s = sr.ReadLine();
            sr.Close();

            DataExport("Select * from PATIENT_TABLE where [BARCODE] = '" + s + "'", filename);
        }
        catch { }
    }

    public void DataExport(string SelectQuery, string filename)
    {
        try
        {
            using (var dt = new DataTable())
            {
                using (var da = new SqlDataAdapter(SelectQuery, con))
                {
                    da.Fill(dt);
                    var rows =
                        from dr in dt.Rows.Cast<DataRow>()
                        select String.Join(
                            ",",
                            from dc in dt.Columns.Cast<DataColumn>()
                            let t1 = Convert.IsDBNull(dr[dc]) ? "" : dr[dc].ToString()
                            let t2 = t1.Contains(",") ? String.Format("\"{0}\"", t1) : t1
                            select t2);

                    using (var sw = new StreamWriter(filename))
                    {
                        // sw.WriteLine(header);
                        foreach (var row in rows)
                        {
                            sw.WriteLine(row);
                        }
                        sw.Close();
                    }
                }
            }
        }
        catch (Exception e) { Console.WriteLine(e.Message); }
    }

    public void WriteFileOutput()
    {
        string path = @"C:\Users\Priya\Desktop\Project\Data.txt";
        if (File.Exists(path))
        {
            string[] lines = File.ReadAllLines(path);

            foreach (string line in lines)
            {
                Console.WriteLine(line);
            }
        }
        Console.ReadLine();
    }

    public void  timer()
        {
            aTimer = new System.Timers.Timer();

            aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
            aTimer.Interval = 10000;
            aTimer.Enabled = true;

            Console.WriteLine("Press the Enter key to exit the program.\n");
            Console.ReadLine();
        }

    private static void OnTimedEvent(object source, ElapsedEventArgs e)
        {
            //System.Windows.Forms.SendKeys.Send("{ENTER}");
            Console.WriteLine("5 seconds Elapsed at {0} ", e.SignalTime); // for your reference to check every five seconds
        }

    public static void Main(string[] args)
    {             
      Program p = new Program();     
      p.getConsoleInput();
      p.ReadWriteIntoFile();
      p.WriteFileOutput();
    }   
}

}

1 个答案:

答案 0 :(得分:0)

Priyanka,欢迎来到SO。我将采取的方法是以编程方式发出telnet并等待来自telnet的响应。现在,您登录时ConsoleApplication2不知道会话。

因此,这是解决方案的高级方法

  1. 启动您的ConsoleApplication2申请
  2. 使用Telnet库打开与设备的连接
  3. 使用相同的库
  4. 从Telnet读取扫描的响应
  5. 使用响应做数据库事务。
  6. 如果您有Telnet库,问题将变得更加简单。但是,SO中有一个类似的问题,这里推荐了一个库 - Executing commands by using Telnet in C#

    希望这有帮助!