ZiBaT => Peter Levinsky => Tech => exercise
Introduction
Simple Server - Echo Server
Updated : 2017-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 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 2015/2017   Create a new solution 'ConsoleApp (framework)', and name it e.g. 'EchoServer'.
    (NB! in 2017 do NOT choose 'ConsoleApp .Net Core', which have restricted API's)
  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.

Assignment B: Refactor Your Server

  1. Refactor your server so all the code to handle your client is extracted in a method 'DoClient'.
  2. Commit your project.

Assignment C:  An Echo Client

  1. In your solution create a new project (AGAIN 'ConsoleApp (framework)' and NOT 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 = IPAddress.Loopback 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.

Assignment D:  Run 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 from 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.