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
- 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')
- Create a class 'Server' and create a method 'Start'.
- In the start method
- 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)).
- Start the TcpListener.
- 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)
- On the TcpListener 'AcceptTcpClient()' and name it 'socket'.
- Make the two cnnection channel reading and writing i.e. 'new StreamReader(socket.GetStream())' & 'new StreamWriter(socket.GetStream())'.
- Read a line from your stream-reader ('ReadLine()') save it in a String and name it 'line'.
- Write back the line to your stream-writer ('WriteLine(line)') -- then remember to 'Flush()' the stream-writer.
- In Main create an object of your Server-class and call the method 'Start()'.
- Optional: Commit and push
your
server-solution to a Git-Repository.
- Try your server with the Socket-Test program - for installation see tools
Assignment B: Refactor Your Server
-
Refactor your server so all the code to handle your client is extracted in a method 'DoClient'.
- The signature of the method should be similar to:
public void DoClient(TcpClient socket)
- Put all your code of StreamReader, StreamWriter and ReadLine, WriteLine in this method
- Try with SocketTest program to see it still works.
- Optional: Commit your project.
Assignment C: An Echo Client
-
In your solution create a new project (AGAIN 'ConsoleApp (.Net 5)' and name it 'EchoClient'.
- Create a class 'Client' and create a method 'Start'.
- In the start method
- Open for a new socket and for reading and writing -- All these should be in an using-statement
- Create a TcpClient with the parameter IP = "localhost" and PORT = 7 or 7777 and name it 'socket'.
- Make the two streams into reading and writing i.e. 'new StreamReader(socket.GetStream())' & 'new StreamWriter(socket.GetStream())'.
- Write a line to your stream-writer ('WriteLine(<<Your line to be echoed e.g. "Peter">>)') -- then remember to 'Flush()' the stream-writer.
- Read a line from your stream-reader ('ReadLine()') save it in a String and name it 'line'.
- Print out the line in the console window ('Console.WriteLine(line)).
- In Main create an object of your Client-class and call the method 'Start()'.
- Optional: Commit and push
your
Client-solution to a Git-Repository.
- Try your Client with the Socket-Test program - for installation see tools
Assignment D: Run Both the server and the Client
-
In your solution
- Start your server
- 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
- Refactor the server to count the number of words sent to the server and send back the number.
- Try this new feature.
- Optional: Commit and push
your
Client-solution to a Git-Repository.
Assignment Extra A: Consider how to test your server
- Consider how to test your server code.
- Make a unit test of your server.