Zealand => Peter Levinsky => Tech => exercise
Extra Simple Server - mini Chat
Updated : 2019-08-26

Extra 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 UWP APP-application.

Background:

Exercise Simple Server and Exercise Cuncurrent Server

 

Assignment A: An Mini Chat Server

  1. In Visual Studio   Create a new solution 'ConsoleApp (.Net Core)', and name it e.g. 'MiniChatServer'.
  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'
          Make the server concurrent i.e. have a loop around AcceptTcpClient and call DoClient within a Task
      2. In DoClient
        1. From the TcpClient 'GetStream()' and name it 'ns'.
        2. Wrap the stream into reading and writing i.e. 'new StreamReader(ns)' & 'new StreamWriter(ns)'.
        3. In a loop i.e. echo more than one line
          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 client socket
             
    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:  An MiniChat Client

    1. In your solution create a new project (AGAIN '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 = "localhost" 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 C:  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 Extra A (Hard): 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.