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 RAZOR PAGES -application.
Background:
Exercise Simple Server and Exercise Cuncurrent Server
Assignment A: An Mini Chat Server
- In Visual Studio Create a new solution 'ConsoleApp (.Net 5)', and name it e.g. 'MiniChatServer'.
- Create a class 'Server' and create a method 'Start'.
- In the start method
- Create a TcpListener with the parameter IP = IPAddress.Loopback and PORT = 7070 .
- Start the TcpListener.
- in a Server loop - to handle more than one client
- 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 server concurrent i.e. have a loop around AcceptTcpClient and call DoClient within a Task
- In DoClient
- From the TcpClient 'GetStream()' and name it 'ns'.
- Wrap the stream into reading and writing i.e. 'new StreamReader(ns)' & 'new StreamWriter(ns)'.
- In a loop
i.e. echo more than one line
- Read a line from your stream-reader ('ReadLine()') save it in a String and name it 'line' and print it on teh screen (Console).
- Read a line from the console (Console.ReadLine()) save it in a string (e.g. 'myLine')
- Write back the 'myLine' to your stream-writer ('WriteLine(line)') -- then remember to 'Flush()' the stream-writer.
- Until you either 'line' or 'myLine' is "STOP" then stop and close the client socket
- In Main create an object of your Server-class and call the method 'Start()'.
- Commit and push your server-solution to a Git-Repository.
Assignment B: An MiniChat Client
-
In your solution create a new project (AGAIN 'ConsoleApp (.Net 5)' and name it 'MiniChatClient'.
- 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 = 7070 and name it 'socket'.
- From the TcpClient 'GetStream()' and name it 'ns'.
- Wrap the stream into reading and writing i.e. 'new StreamReader(ns)' & 'new StreamWriter(ns)'.
- in a Loop
- Read a line from the console
- Write the line to your stream-writer -- 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)).
- Until either
you own line or the incomming list is "STOP"
- In Main create an object of your Client-class and call the method 'Start()'.
- Commit and push
your
Client-solution to a Git-Repository.
Assignment C: Run the server and the Client
-
In your solution
- Start your server
- start your client
- Try to start another client - what happen ?
-
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.