In this exercise you must design and program a Friend2Friend chat application.
This application is pure Peer2Peer - there is no central chat server, nor is there any central registry (aka. index) server.
The idea is that you run the Friend2Friend chat application - and so does your friend. You connect to your friend and you start chatting (sending text messages to each other).
The user interface might look as ugly as this one:
Each peer plays the role of a client and a server.
The client role:
The server role
TCP is used for transportation of messages.
TCP connections are open for as long as the chat conversation is running (long connections).
This is unlike Echo and HTTP/1.0 where we use short connections (one request + one response and connection closes).
You probably need to go through a series of steps to make this application run ...
Make the user interface. Use my (ugly) user interface for inspiration.
Make a WPF project in Visual Studio. WPF allows for more network related APIs than Windows Store, etc. does.
You can even get the XML code for my (ugly) user interface, right here.
This is what happens when the user presses the Start button.
This is what happen when the user presses the Connect button
_writers
_writers
is a List<StreamWriter> shared between the client role and the server role.
This is what hapens when another peer connects to this peer
StreamWriter
. StreamWriter
object in a List named _writers
This is what happens whan the user presses the Send button.
_writers
.In WPF only the Main() thread/task is allowed to update the GUI. Any other thread/task must NOT update date GUI (the GUI might freeze or throw strange exceptions at the user ...).
The solution to this problem is
Dispatcher.Invoke(() => MessageOutput.AppendText(line + "\n"));
The Dispatcher.Invoke(...) method has a single Action parameter (here expressed using Lambda). This action is added the exeucted by the Main() thread.
Use the Trace class to keep track of what is going on in the Peer.
ConsoleTraceListener is not very interesting in a GUI application which has no console window.
I have made special TextBoxTraceListener class which extends the TraceListener class and writes messages to a TextBox (the Status TextBox in my ugly GUI).
You can get the code for the TextBoxTraceListener.
Yet another issue: How to we know who want to chat?
Until now you had to ask for your chat partners IP + port.
Another ideas is to broadcast a UDP message on the local network asking for possible chat partners.