Exercise: Friend2Friend chat

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:

P2Pchat

General architecture

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).

Programming it ...

You probably need to go through a series of steps to make this application run ...

User interface

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.

Peer starts the server part

This is what happens when the user presses the Start button.

Client role: Peer makes a connection to another peer

This is what happen when the user presses the Connect button

Server role: Peer receives a connection from another peer

This is what hapens when another peer connects to this peer

Client role: Peer sends a message

This is what happens whan the user presses the Send button.

Client role: Peer receives a message

An issue: Updating the GUI

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.

Another issue: Tracing

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.

Extra: UDP who wants to chat?

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.