ZiBaT => Peter Levinsky => Tech => exercise
Introduction
Simple Server - Echo Server
Updated : 2017-08-26

More Server - An Echo Plus Server (mini chat)

Mission:

To design and program two programs one to be an minichat-server and one to be an minichat-client
Again NO APP-application.

Background:

Exercise Simple Server

For programming en iterative server in C# Console Application

Assignment A: An Mini Chat Server

  1.   In Visual Studio 2015/2017   Create a new solution 'ConsoleApp (framework)', and name it e.g. 'MiniChatServer'.
    (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 = 7070 .
    2. Start the TcpListener.
    3. in a Server loop - to handle more than one client
      1. 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)'.
      2. In a loop
        1. Read a line from your stream-reader ('ReadLine()') save it in a String and name it 'line' and print it on teh screen (Console).
        2. Read a line from the console (Console.ReadLine()) save it in a string (e.g. 'myLine')
        3. Write back the 'myLine' to your stream-writer ('WriteLine(line)') -- then remember to 'Flush()' the stream-writer.
        4. Until you either 'line' or 'myLine' is "STOP" then stop and close the server
  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 MiniChat Client

  1. In your solution create a new project (AGAIN 'ConsoleApp (framework)' and NOT ConsoleApp .Net Core, and name it 'MiniChatClient'.
  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 = 7070 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. in a Loop
      1. Read a line from the console
      2. Write the line to your stream-writer -- 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. Until either
      6. you own line or the incomming list is "STOP"
    3. In Main create an object of your Client-class and call the method 'Start()'.
    4. 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
  2. Try to start another client - what happen ?
  3. Can you have a comminication with one client - and at the same time with another?

Assignment E:  Refactor the server

  1. Refactor the server to make use of the DoClient in a seperate thrad / Task i.e. Task.Run(DoClient(socket)).
  2. Try the assignment D again - how is it now - can you handle two(many) client at the same time?
  3. Commit and push your Client-solution to a Git-Repository.

 

Assignment Extra A: Refactor your Server and Client to work asynchronous

The server and the client should be able to sent or receive several messages before receive og sent a message e.g. sent two messages then receive three

Hint You need to use a Task for sending and another for receiving - so you will get a lot of tasks.

Assignment Extra B: Refactor your Server to work like a simple HTTP-server

The server (port 8989) should read the first line in a http request (see the book) split the requestline in three (method, uri, version)

If the method is get - then open the file specified in the uri and send it back - compling the http Response (see the book)

send a response line (version(you case always 1.0), statuscode, status msg), blank line, then the body i.e. the file to be sent back

You can try your server from any browser. e.g. localhost:8989/testfile.txt