Connect to MSN Messenger Using the MSN Protocol: Part 1

Download Source Code

The MSN messenger protocol allows developers to create their own version of MSN. There are many reasons why you may want to develop your own Messenger version. It could be for organisational purposes or to add more features. This article explains how to use the Messenger protocol. This article only explains how to get authenticated. It does not explain how to send messages between users. You can also download the source sample to get a better understanding.

Connecting To MSN

To successfully log in to MSN Messenger, you need to get authenticated. This happens in three phases. In every phase, the program you develop (Client) sends a series of commands to an MSN server. The server responds with a series of command, which consists of a number of parameters. Look at the three phases.

Authentication Server

The first thing you need to do is connect to the authentication server. The original MSN client uses the host messenger.hotmail.com and port 1863. The code below uses the TcpClient Class from the System.Net.Sockets package to connect to the host messenger.hotmail.com using port 1863.

TcpClient msnTcp = new TcpClient();
try
{
   msnTcp.Connect("messenger.hotmail.com", 1863);

   if (msnTcp.Connected)
   {
      //Client Connected.
   }
   else
   {
      //Client Not Connected.
   }
}
catch (UriFormatException UFE)

{
   //Process UriFormatException here.
}

Once you are connected to the authentication server, you need to send the first of many commands to the server. Every command you send to the server must include a transaction ID. A transaction ID is a number that increments by one every time you send a command to the MSN server. The response from the server includes the transaction ID that was sent previously to the server. This is to acknowledge that the server is responding to the correct command. Begin with the first command you need to send to the MSN server; this is the version command. The client (in this case, your program) sends the versions that it can support to the server and waits for the server to respond. The command looks like the following.

VER 1 MSNP8 CVR0

Before you can use this command, you need to setup a NetworkStream, StreamReader, and StreamWriter. The code below initalises the three Classes mentioned.

NetworkStream networkStream = msnTcp.GetStream();
StreamWriter streamWriter   = new StreamWriter(networkStream);
StreamReader streamReader   = new StreamReader(networkStream);

A NetworkStream object is used to send and receive byte data over the TCP/IP. This is done using TcpClient.GetStream(). The StreamReader Class is used to read the response from the server and the StreamWriter Class is used to send data to the server.

You now can use the streamWriter object to send your first command to the server. The following code sends the VER command to the server with a transaction ID. Note the transaction ID starts with 1.

streamWriter.WriteLine("VER 1 MSNP8 CVR0");
streamWriter.Flush();

After sending the command, you need to read the response from the server. This is done using the following code.

string strVersion = streamReader.ReadLine();

At this stage, the Client and the server have agreed on a version in which they will communicate. You can use a rrrra message box to display the response. The response should be something like the following.

VER 1 MSNP8 CVR0

Notice that the server has responded with the same transaction ID that was included in the VER command.

Next, you need to send a CVR command. The CVR command sends version information about a client and operating system to the server. The server will reply with information about the version of the client that users are currently recommended to use. The CVR command includes information about the language you speak, the name and version of your client, and the name and version of your OS. The CVR command has a Transaction ID and 8 parameters. Details of the parameters are listed below.

Parameter Number Description Example
1 A hexadecimal number specifying your locale identifier “0x0409” For U.S. English
2 Your OS type “win” for Windows
3 Your OS version “4.10” for Windows 98
4 The architecture of your computer “i386” for Intel-compatible PCs of type 386 or above
5 Your client name “MSMSGR” for the official MSN Messenger client
6 Your client version “6.0.0602”
7 Always “MSMSGS” in the official client  
8 Your passport  

The following code uses the streamWriter object to send the CVR command.

streamWriter.WriteLine("CVR 2 0x0409 win 4.10 i386 MSNMSGR
                       6.2.0208 MSMSGS yourPassport@hotmail.com");
streamWriter.Flush();

Change yourPassport@hotmail.com to the name of your hotmail passport. The code below will use the streamReader object to read the response from the server.

string strCVR = streamReader.ReadLine();

The server will respond with a message like the following.

CVR 2 7.0.0816 7.0.0816 6.2.0208 http://msgr.dlservice.microsoft.com/
   download/4/b/c/4bc83bb2-18dd-486f-943f-332a9b3e01dc/
   Install_MSN_Messenger_DL.exe http://messenger.msn.com

The CVR response from the server includes five parameters:

  • The first parameter is a recommended version of the client for you to use, or “1.0.0000” if your client information is not recognised.
  • The second parameter is identical to the first.
  • The third parameter is the minimum version of the client that is safe for you to use, or the current version if your client information is not recognised.
  • The fourth parameter is a URL to download the recommended version of the client.
  • The fifth parameter is a URL where you can go to get more information about the client.

After receiving the response to the CVR command, you need to send a USR command. The USR command consists of three parameters. The first parameter is the authentication system; this is TWN. The second parameter is the letter I (Initiating authentication). And finally, the third parameter is the account name that you want to log on with.

The server will respond with an XFR. This indicates that you should now connect to the Notification Server. The IP address and port number are included in the response. The code below will send the USR command using the streamWriter object.

streamWriter.WriteLine(""USR 3 TWN I yourPassport@hotmail.com");
streamWriter.Flush();

Now, get the response from the server using the streamReader object.

string strUSR = streamReader.ReadLine();

The server will respond with a message like the following.

XFR 3 NS 207.46.106.83:1863 0 65.54.239.140:1863

This message is very important because it indicates the IP address and Port number to connect to the Notification Server (NS). The IP address in this case is 207.46.106.83 and the port number is 1863. You need to extract this information from the response. The following code will extract the IP address and the port number from the response.

Note: The IP address and port number that follows after the 0 indicates the IP address of the current server you are connected to.

string[] split_server_response;
   split_server_response = strUsr.Split(' ');
   string[] split_ip_from_port;
   split_ip_from_port  = split_server_response[3].Split(':');
   string strNSAddress = split_ip_from_port[0];
string strNSPort       = split_ip_from_port[1];

The strNSAddresss variable now stores the IP address to the Notification Server. The variable strNSPort contains the port number.

Phase one is now complete. You need to move on to phase two. Phase two is very similar to phase 1. You connect to the new Notification Server (NS) using the IP address and port number obtained from phase one. This phase gets you connected and logs you in using your passport password. The Notification Server (NS) contains information such as your status. It also tells you which of your contacts are online.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read