Creating a .NET Instant Messenger
Instant messaging is sweeping the worldit is rapidly replacing email as the preferred electronic communications means. Behind this phenomenon, however, is a rather simple technology, especially with .NET's new programming paradigm. This article will give some background on the technologies needed to build an instant messenger, as well as build a simple one step by step.
The Network
The first thing that comes to mind when thinking about an instant messaging application (IM) is the Internet; after all, to send an instant message, you need to deal with a network. The System.Net namespace in the .NET Framework contains all the classes you'll ever need to handle network requests of any type (and for IM, you don't need many).
.NET introduces pluggable protocols, which is a way to create an application that can handle any type of Web request using only one class. .NET automatically determines the type of network traffic that is going on, and adjusts appropriatelytaking a lot of work away from you, the developer.
In this article, we'll look at a few classes in the System.Net.Sockets namespace, which provides a more sophisticated interface for dealing with network traffic. You'll find, though, that even these are very simple.
For an IM application, there are two classes we're interested in. System.Net.Sockets.TcpClient and System.Net.Sockets.TcpListener provide methods for you to send and receive data, respectively, using the TCP protocol. All you need to know is the IP address of the person you want to communicate with, and you're set to go.
But wait a moment. An application cannot do two things at once. That is, your application cannot both send messages and receive them at the same time, which is a key feature of instant messaging. Somehow, you have to devise a way to accomplish two things at oncewe'll discuss this in the next section.