using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;

namespace EchoClientSampleCode
{
    class Program
    {
        private static string hostname = "ServerAddress";
        private static int port = ServerPort;
        private static TcpClient tc;
        private static NetworkStream ns;
        private static StreamReader sr;
        private static StreamWriter sw;
        private static string input;

        static void Main(string[] args)
        {
            try
            {
                tc = new TcpClient();
                tc.Connect(hostname, port);
            }
            catch (Exception exc)
            {
                Console.WriteLine("Couldn't to connect...");
            }

            ns = tc.GetStream();
            sr = new StreamReader(ns, System.Text.Encoding.Default);
            sw = new StreamWriter(ns, System.Text.Encoding.Default);

            try
            {
                while (tc.Connected)
                {
                    input = Console.ReadLine();
                    if (input == "quit") break;
                    sw.WriteLine(input);
                    sw.Flush();
                    Console.WriteLine(sr.ReadLine());
                }
            }
            catch (Exception exc)
            {
                Console.WriteLine(exc.Message);
            }

            Console.WriteLine("Disconnecting from server...");
            sr.Close();
            sw.Close();
            ns.Close();
            tc.Close();
            Console.WriteLine("Press Enter key.");
            Console.ReadLine();
        }
    }
}

+ Recent posts