Zealand => Peter Levinsky => Tech => exercise
Introduction
Simple Server - Echo Server
Updated : 2021-08-30

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 RAZOR PAGE-application.

Background:

CN chapter 2.7 p. 189-195

For programming server and client in C# Console Application

Assignment A: An Echo Server

  1.   In Visual Studio 2019   Create a new solution 'Console App (.Net 5)', 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. Make the two cnnection channel reading and writing i.e. 'new StreamReader(socket.GetStream())' & 'new StreamWriter(socket.GetStream())'.
    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. Optional: 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. Optional: Commit your project.

Assignment C:  An Echo Client

  1. In your solution create a new project (AGAIN    'ConsoleApp (.Net 5)' 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. Make the two streams into reading and writing i.e. 'new StreamReader(socket.GetStream())' & 'new StreamWriter(socket.GetStream())'.
    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. Optional: 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. Optional: 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.