In this series we are going to be looking at TCP IP, Client / Servers and what a massive world of opportunities this knowledge opens up to us, Today we will be doing a Chat System!
The sample is built in Visual Studio 2012 Ultimate as an x86 targeted application using .Net Framework 4. We will be using NuGet packages, a number of 3rd party libraries. All of which will be fully explained to you ensuring that the final compilation of your App will be hassle free. Oh! And the sample code is verbosely commented so you should have no problem in working out what the code does.
Communication is fundamental to us all. But it is also fundamental to many programs. When your application connects with a database it has to communicate, when your browser connects to a website, it communicates and the vast majority of the communication that occurs on the internet is does through TCP communication. In this first example we will write a client and an “echo server” which simply replies with the message you sent – pretty much like the ping command. It doesn’t sound like much but it is the basis that we will start from.
Open Visual Studio 2012 and Create a New Project. Call the Project ChatApp. You will find in the Solution download a new .dll called NetComm it is a 3rd party library that we will be using for our communication. Or you can download it from the link below in the More Information section.
We have a single Toolstrip with a button which will create new Clients for us. This just makes things a little simpler for us at this stage.
We have a RichTextBox which shows what the Server is doing and
A StatusStrip which shows how many Connections (clients) are currently being processed by the Server.
I will leave you do build your own design, and that of the Clients.
The Server is instantiated and a number of events are subscribed to. We then start the Server and wait for the Events to be fired so we can process them. The Events will only be fired when a client connects. You can simulate clients connecting by clicking on the button at the top of the Server window. You can have as many clients as you like and delete clients as you like as well.
All messages sent by the clients are sent through the server. At this point the server looks to see if it is a command or a simple message for other’s to read. In this implementation a message sent by one client is displayed on all other connected clients screens. Later we will change this.
The only command the Server currently recognises is “CLOSING” which is sent from a client when it is going off line. The Clients also recognise a single command which is “CList” – this tells the client which friends are online – in this present version that is every client that is connected to the Server.
To convert the solution to a previous version of Visual Studio you can use this free application: http://vsprojectconverter.codeplex.com/ or download and use Visual Studio 2013 Express which is freely available from Microsoft from here: http://www.visualstudio.com/downloads/download-visual-studio-vs.