Пример #1
0
    public static void Main(String[] args)
    {
        EchoClient conversant=null;
         StreamWriter swriter=null;
         StreamReader sreader=null;
         try {
        //Host name comes from command line
        //If no host specified, local machine is host
        String host=args.Length==1?args[0]:"127.0.0.1";

        //Connect to Echo server
        conversant=new EchoClient(host);

        //Get the stream between server and client
        NetworkStream ns=conversant.GetStream();

        //Create a user-friendly StreamWriter
        swriter=new StreamWriter(ns);

        //Create a user-friendly StreamReader
        sreader=new StreamReader(ns);

        //Prompt user for message to send to server
        //Period "exit" tells server to end session
        String input;
        Console.Write("Enter screen name: ");

        while ((input=Console.ReadLine())!="exit") {
        //Send message to server
        swriter.WriteLine(input);
        swriter.Flush();

        //Get the Echo from the server
        String returndata=sreader.ReadLine();
        Console.WriteLine("Reply from "+host+": "+returndata);
        Console.Write("Enter text: \"exit\" to stop: ");
        }

        //Send final message and scram
        swriter.WriteLine(".");
        swriter.Flush();

         } catch (Exception e) {
        Console.WriteLine(e+" "+e.StackTrace);
         } finally {
        //Close the connection whether exception thrown or not
        if (swriter!=null) swriter.Close();
        if (sreader!=null) sreader.Close();
        if (conversant!=null) conversant.Close();
         }
    }
Пример #2
0
    public static void Main(String[] args)
    {
        EchoClient client = null;

        try
        {
            handler += new EventHandler(Handler);
            SetConsoleCtrlHandler(handler, true);

            //Host name comes from command line
            //If no host specified, local machine is host
            String host = args.Length == 1 ? args[0] : "127.0.0.1";

            //creates connection
            client = new EchoClient(host);

            //gets stream between server and client
            NetworkStream ns = client.GetStream();

            //creates writer and reader
            swriter = new StreamWriter(ns);
            sreader = new StreamReader(ns);

            //tells the user how to exit the chat properly
            Console.Write("Enter text: \"exit\" to leave the chat: \n");

            //prompts the user the screen name for this client
            String input;
            Console.Write("Enter screen name: ");

            //starts thread to read messages being sent from other clients
            //from the server
            Thread readthread = new Thread(new ThreadStart(client.Reader));
            readthread.Start();

            while ((input = Console.ReadLine()) != "exit")
            {
                //sends the message to the sever to be sent to
                //other clients
                swriter.WriteLine(input);
                swriter.Flush();
            }

            //tells the server that the client has left
            swriter.WriteLine("exit");
            swriter.Flush();

            //tell reader to stop reading
            endreader = true;

            //holds the console while it cleans up from exiting without typing "exit"
            while (!exitSystem)
            {
                Thread.Sleep(500);
            }

        }
        catch (Exception e)
        {
            Console.WriteLine(e + " " + e.StackTrace);
        }
    }