Connect to MSN Messenger Using the MSN Protocol: Part 1

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.

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.

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.

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.

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.

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.

Now, you can begin to send commands to the Notification Server (NS). Once again, you need to send the VER, CVR, and USR commands. You can copy and paste the same code from phase one. The code below will send the VER, CVR, and USR commands to the Notification Server (NS).

Again, replace yourPassport@hotmail.com with your own passport account. Now, in phase one, when you reached this stage of the commands, the server responded with an XFR command that indicated that you should connect to the Noftication Server (NS). But this time, the server will respond with a ChallengeString. However, in cases where the server is busy or is down for maintenance, the server may respond with another XFR command indicating that you should connect to another Notification Server (NS). For this article, you should assume that the server responded with a ChallengeString.

For the authenication of your passport account to be verified, you need to generate a Ticket using the ChallengeString. The Ticket is generated using your account (yourPassport@hotmail.com), password, and the ChallengeString. I will not explain this part; however, the download includes a Class named MSN_GetTicket.cs. You can read more about the Ticket from this link.

After generating the Ticket, you need to send a USR command to the Notifcation Server that includes the Ticket. The code below will send a USR command with a Ticket to the Notification Server.

If all is successful, the server should respond with a status OK message. The message should include your passport account and your MSN Friendly name.

Phase two is now complete and you can move to phase three. Phase three is the Dispatch Server.

Dispatch Server

The Dispatch server allows you to send messages to your online friends. You need to send Commands to the Dispatch Server (DS) indicating the user you want to have an instant message with.

As I mentioned at the beginning of this article, I will only deal with the authentication part. I will write a separate article on the Dispatch Server at a later date. The download includes three MSN Classes.

MSN_Authentication.cs

MSN_NotificationServer.cs

MSN_GetTicket.cs

Although there may seem to be a lot of code for an authentication, the code is actually very simple. I have included methods in the Classes, that get the response from the server so that you can see the responses from the server.

Top White Papers and Webcasts

U.S. companies are desperately trying to recruit and hire skilled software engineers and developers, but there is simply not enough quality talent to go around. Tiempo Development is a nearshore software development company. Our headquarters are in AZ, but we are a pioneer and leader in outsourcing to Mexico, based on our three software development centers there. We have a proven process and we are experts at providing our customers with powerful solutions. We transform ideas into reality.

When individual departments procure cloud service for their own use, they usually don't consider the hazardous organization-wide implications. Read this paper to learn best practices for setting up an internal, IT-based cloud brokerage function that service the entire organization. Find out how this approach enables you to retain top-down visibility and control of network security and manage the impact of cloud traffic on your WAN.