Zealand => Peter Levinsky => Tech => exercise
Introduction
Simple Server - Echo Server
Updated : 2019-08-26

Simple Server - An Echo Server

Mission:

To design and program two programs one to be an echo-server and one to be an echo-client.
Do not implement a gui with 'bells and whistles' only plain screen. NO UWP APP-application.

Background:

CN chapter 2.7 p. 185-187 + 192-198

For programming server and client in C# Console Application

Assignment A: An Echo Server

  1.   In Visual Studio 2017   Create a new solution 'Console App (.Net Core)', and name it e.g. 'EchoServer'.
    (Please! do NOT choose 'Console App framework')
     
  2. Create a class 'Server' and create a method 'Start'.
     
  3. In the start method
    1. Create a TcpListener with the parameter IP = IPAddress.Loopback and PORT = 7 or 7777 (port 7 is the stadard for echo server (see RFC 862)).
    2. Start the TcpListener.
    3. Open for incomming socket and for reading and writing -- All these should be in an using-statement (This give you the benefit of indirrect closing all sockets and streams in the using-statement)
      1. On the TcpListener 'AcceptTcpClient()' and name it 'socket'.
      2. From the TcpClient 'GetStream()' and name it 'ns'.
      3. Wrap the stream into reading and writing i.e. 'new StreamReader(ns)' & 'new StreamWriter(ns)'.
    4. Read a line from your stream-reader ('ReadLine()') save it in a String and name it 'line'.
    5. Write back the line to your stream-writer ('WriteLine(line)') -- then remember to 'Flush()' the stream-writer.
       
  4. In Main create an object of your Server-class and call the method 'Start()'.
     
  5. Commit and push your server-solution to a Git-Repository.
     
  6. Try your server with the Socket-Test program - for installation see tools

Assignment B: Refactor Your Server

  1. Refactor your server so all the code to handle your client is extracted in a method 'DoClient'.
     
  2. The signature of the method should be similar to:

                 public void DoClient(TcpClient socket)
     
  3. Put all your code of StreamReader, StreamWriter and ReadLine, WriteLine in this method
     
  4. Try with SocketTest program to see it still works.
     
  5. Commit your project.

Assignment C:  An Echo Client

  1. In your solution create a new project (AGAIN    'ConsoleApp (.Net Core)' and name it 'EchoClient'.
     
  2. Create a class 'Client' and create a method 'Start'.
     
  3. In the start method
    1. Open for a new socket and for reading and writing -- All these should be in an using-statement
      1. Create a TcpClient with the parameter IP = "localhost" and PORT = 7 or 7777 and name it 'socket'.
      2. From the TcpClient 'GetStream()' and name it 'ns'.
      3. Wrap the stream into reading and writing i.e. 'new StreamReader(ns)' & 'new StreamWriter(ns)'.
    2. Write a line to your stream-writer ('WriteLine(<<Your line to be echoed e.g. "Peter">>)') -- then remember to 'Flush()' the stream-writer.
    3. Read a line from your stream-reader ('ReadLine()') save it in a String and name it 'line'.
    4. Print out the line in the console window ('Console.WriteLine(line)).
    5. In Main create an object of your Client-class and call the method 'Start()'.
    6. Commit and push your Client-solution to a Git-Repository.
       
  4. Try your Client with the Socket-Test program - for installation see tools

Assignment D:  Run Both the server and the Client

  1. In your solution
    1. Start your server
    2. start your client

Hint: To make it more smooth; Open the properties for the solution, Then choose multiple startup projects and configure to start first.
Remember to have the server up before the client. -- Now you can 'just' push the green arrow and both projects will start.

Assignment E:  Refactor the server

  1. Refactor the server to count the number of words sent to the server and send back the number.
     
  2. Try this new feature.
     
  3. Commit and push your Client-solution to a Git-Repository.

 

Assignment Extra A:  Consider how to test your server

  1. Consider how to test your server code.
  2. Make a unit test of your server.