Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have created 1 console Application and it is running perfectly. In my project I have to take input string and from telnet specific window named as "telnet 192.168.x.x" and run my project and give output related to that string (i.e barcode code is all I have to receive on telnet window and give output (details of barcode code) related to that code in same telnet window). I am able to run this console app in cmd.exe and ConsoleApplication.exe but now I am passing barcode string manually then how to run this application on telnet window and receive barcode string automatically? please help .

Thanks in Advance

here is my full code

namespace ConsoleApplication2
{
    class Program
    {
        string path = @"text file path";

        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["constring"].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 ReadWriteIntoFile()
        {
            try
            {
                string filename = @"txt file path";
                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 = @"text file path";
            if (File.Exists(path))
            {
                string[] lines = File.ReadAllLines(path);

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

            }
            Console.ReadLine();
        }

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

        public void timerfor()
        {
            Timer t;
            t = new System.Timers.Timer(10000);
            // Hook up the Elapsed event for the timer. 
            t.Elapsed += OnTimedEvent;
            t.Enabled = true;
        }

        private static void OnTimedEvent(Object source, ElapsedEventArgs e)
        {
            Console.WriteLine(ConsoleKey.Enter);          
        }
    }
}
<pre lang="c#">
Posted
Updated 25-May-15 20:37pm
v3
Comments
Richard MacCutchan 18-May-15 6:05am    
Your question is not clear; what does telnet have to do with this?
Member 11543226 19-May-15 1:10am    
when device scans barocde of any product ,on telnet server window i.e (telnet 192.168.x.x 23) barcode number appears like XE0318992 and i have to capture this string and show details of product related to this barcode on the same server window. this console app exactly do this task but now i manually passing barcode number because now i am able to run this app on visual studio command prompt and cmd.exe but when i get barcode number after scanning device i have to receive it from telnet server window . so i want the way how to capture barcode number in this program from telnet window and show output on same.
Richard MacCutchan 19-May-15 4:53am    
If I understand what you mean, the scanner is connected to a remote server. You then connect to that server via telnet and need to capture the data from the scanner remotely.

The issue seems to be how you get the data from the scanner, to your client application. I know that some telnet clients can capture any data printed to the console in telnet, so you could try one of those. Alternatively you could write your own client and server applications that communicate via sockets.
Member 11543226 19-May-15 5:05am    
I made client but it does not interact with server properly
Richard MacCutchan 19-May-15 5:43am    
Sorry, but there is no way anyone here can guess what the issue may be. I suggest you open a new question, give full details of what is happening, and where the problem occurs.

1 solution

Wow, it is really a puzzle to read your question. Let me rephrase it for you. You have some data that you receive from Telnet (Barcode number) which you want to use it by your external console application and based on this data you will retrieve some DB info and you want the same to be shown back in the telnet. Well what you have now is the console app which works fine by manually providing this barcode and getting the expected output but you want to integrate this with the telnet. Is that right ????

Assuming my understanding is right, You have to do the following.

1. Rather than trying to read the data from a telnet window, you have to create a TcpClient and listen to the Telnet server in your case I assume its the barcode reader. There are plenty of examples available how to create a simple Telnet client using C#
2. Perform the operations that you need to perform and show the results in your console window itself.
 
Share this answer
 
Comments
Member 11543226 26-May-15 5:26am    
Sorry sir for making my question complicated to understand. but one of my colleague works with device and provide me connection and did all these stuffs in embedded programming I made tcp client for communicating with this telnet server but wont work, so I cant understand all this. Just have to do it by own assumption and learning.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900