All pastes #43351 Raw Edit

Anonymous

public text v1 · immutable
#43351 ·published 2006-02-27 14:59 UTC
rendered paste body
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

public class TelnetReader {

        public static void Main(string [] args) {

                Uri uri = new Uri(args[0]);

                TcpClient client = new TcpClient();

                client.Connect(uri.Host, uri.Port);

                NetworkStream stream = client.GetStream();
                StreamWriter writer = new StreamWriter(stream);
                StreamReader reader = new StreamReader(stream);

          bool wcontinue = true;
                string tmp;
                try {
                        while (wcontinue) {
                                tmp = Console.ReadLine();
                                if (tmp == "EOF") {
                                        wcontinue = false;
                                } else {
                                        WriteLine(writer, tmp);
                                        Read(reader);
                                }
                        }

                } catch (ApplicationException e) {
                        Console.Error.WriteLine("Got unexpected data: {0}", e.Message);

                } finally {
                        stream.Close();
                }
        }

        private static string Read(StreamReader reader) {
                StringBuilder buffer = new StringBuilder();
                while (reader.Peek() != -1) {
                        buffer.Append(reader.ReadLine());
                        buffer.Append(Environment.NewLine);
                }
                Console.WriteLine(buffer.ToString());
                return buffer.ToString();

        }

        private static void WriteLine(TextWriter writer, string line,
                        params object [] args) {
                writer.WriteLine(line, args);
                writer.Flush();
                //Console.WriteLine(line, args);
        }
}