Published using Google Docs
C# Chat Program
Updated automatically every 5 minutes

C# Chat Program

The goal of this lab is to continue our work with threaded programs in C# as well as a brief introduction to networking in C#.  For this lab, you will be creating 2 separate programs: a C# Chat Server and a C# Chat Client.  Both will be C# console applications, and are meant to communicate with each other across a network connection.

Background

Remember, threads allow a program to perform multiple tasks simultaneously.  A game would have limited use if you could not simultaneously move your character and see the environment update around you.  A chat program that only lets you talk or only lets you see what other people are writing would be similarly limited in use.

We are going to be building off of what we know about threads to build a chat application in C#.  For this lab, you will need to also make use of TCP connections between multiple clients and a single chat server.  TCP (Transmission Control Protocol) ensures that our messages will always arrive, and will always reach their destination in order.  This guarantee is useful for things like getting email, reading web pages, and chatting.

To establish a TCP connection, programs need to create sockets.  This just means that we’re opening up communications between 2 machines.  TCP sockets in C# come in 2 main flavors: a client-side TCP socket and a server-side TCP socket.

//Client-side socket

TCPClient client = new TcpClient();

client.Connect(IPAddress, port);

//Server-side socket

TcpListener serverSocket = new TcpListener(IPAddress, port)

The server-side TCP socket opens up a socket and then waits for someone to try to connect to it (they will connect via a client-side TCP socket).  Once the server-side accepts a connection, it will keep that connection open so it can continue to talk to the connected client.

//Server creates a TcpClient to talk to a connected client directly, after accepting the connection

TcpClient newClient = serverSocket.AcceptTcpClient();

This is where threading come in for the server application: the server’s main thread needs to go back to listening for new connections, but you need a thread to keep listening for the client to say something! (it would be a very sad chat client that only allowed a single person to connect).

The client application will create a client-side TCP socket, and try to connect to the server.  Once it has successfully connected, the client application has 2 tasks: first, it needs to listen for messages coming from the server, and second, it needs to be able to respond to commands you type into the console.  You will need at least 2 threads to do this!

Your Task

I have created a GitHub assignment for you that (theoretically) contains starter code for you.  This should consist of a solution with two projects: ClientApp and ServerApp.  Right now, the ClientApp should compile (but do nothing, and have multiple warnings about doing nothing), but the ServerApp should not (too many errors).

Both projects have multiple TODO comments.  TODOs are typically reminders that programmers leave to remind themselves to come back and do something.  TODOs can be common in wizard-generated code, and larger programs.  In this case, I have placed TODOs with instructions on what you need to do to complete these programs.

My goal was to give you enough scaffolding to ensure this project is manageable, but make sure you have enough to do so you get a feel for and can understand the kind of work that goes into writing distributed applications.

The chat application you will be completing relies on sending strings back and forth between the server and any connected clients.  As all client communications occur through this central server, it is known as a centralized client-server application (as opposed as a decentralized peer-to-peer application).

Program Walkthrough

To test your chat, you’ll want to first start the server application.  This should open a server-side socket on an open port (the 0 in the port field simply tries to find an open port).  The server should then print out the IP address of the machine and the port that it has opened for connections.  The server will then wait for clients to contact it.  Once started, you will not need to interact with the server application.

Before starting up a client application, you’ll need to open the properties of the Client project and add in command-line arguments (or run the executable from the console, and provide command-line arguments: myProgram.exe username IP port).  

Once started, your client application should be able to: print out the list of known commands, send a message to an individual user (\whisper), broadcast a message to all connected users, see a list of all currently connected users, and disconnect from the server.

I strongly recommend that you find a buddy to fully test your implementation - their clients should be able to connect to your server, and your clients should be able to connect to their server.

Ready to Start?

https://classroom.github.com/a/6BJIiQGj 

Submission

I believe that you just need to push your changes back into your assignment repo!