In this exercise you must program a simple Math TCP server. The server receives requests like "ADD 4.4 5.5" and send a response with the result ("9.9" in this case).
The structure of the server is very similar to the Echo TCP server. However, the service given by the Math server is slightly more advanced than the service given by the Echo server.
Before you start programming you must define a protocol: A set of rules explaining the structure of requests and responses.
I suggest a single-line request protocol:
The response could be a simple number.
But you can chose another protocol if you want to, for example a multi-line protocol.
Error handling, etc.: Don't worry about this now ... it comes later in this exercise.
Make sure you can read and understand the Echo TCP server.
Make a copy of the Visual Studio Project holding the Echo TCP server (do not modify the original project).
With the Math TCP server you must rework the part of the server where it gives service to the client.
string.Split(separators)
might be usefulDouble.Parse(numberString)
might be usefulMake a simple client program (preferably Console Application in another Visual Studio solution) to try the Math TCP server.
The client program should be very similar to the Echo TCP client program.
Use your unit testing framework to test the Math TCP server.
Eventhough this is not exactly unit testing, the unit testing framework is still useful.
The test assumes that the server is running on a known host + port.
The test send a request to the server (very much like the client program) and make assertions about the response.
In the test you can make helper (not TestMethod) method like
private static String GetResponse(String request) { ... }
The client may send a bad request, i.e. a request not according to the protocol.
Examples:
In any case the server must send back a proper response for example
You will have to adapt the response part of your protocol AND add more lines to your server.
The response should have the following structure:
Does "Analyze Code Coverage" work? Why not?
Add a method Stop()
to the server. The Stop()
methods must stop the server.
stackoverflow.com How to gracefully close TcpListener/TcpClient connection?
Until now the test assumes that the test is running before the test starts
Now you must change so that the test is responsible for starting the server, running the tests, stopping the server.
The unit test annotations [ClassInitialize]
and [ClassCleanUp]
might be helpful, more information.
The server should stop running when it receives a special request like STOP, QUIT or ...
Theory: Discuss why this is generally not a good idea (but today we do it anyway, because it is easy ...)