Click here to Skip to main content
12,898,843 members (54,498 online)
Click here to Skip to main content
Add your own
alternative version


40 bookmarked
Posted 19 Jul 2004

A TCP client that uses MSN protocol

, 19 Jul 2004
Rate this:
Please Sign up or sign in to vote.
A TCP client that uses MSN protocol


This is a project that I'd initiated as a messaging platform for communication. MSN protocol is one of the channels chosen, for its "always up and Internet available" feature.

MSN protocol based messaging is just as any other TCP based client program, it means Winsock programming. With asynchronous processing we will make the program response much better. I'm looking more than this, the program should fork its own thread for each individual MSN ID that is online, so that it can broadcast messages using individual channels. Thus it needs multithreaded processing capability.

I've developed this under VB.Net as .Net had provided handy classes that ease these jobs. Comparing the day of only VC having the strong support for these requirements, I'm still remembering the multithreaded socket based server module that I had developed. But then I've encountered a diff problem.

There are 3 major challenges I've encountered during the development

  1. MSN Protocol communication
  2. MD5 hashing under VB.Net
  3. Multithreading an asynchronous processing TCP Client

Using the code

MSN Protocol communication

  1. There are several sites that maintained information on MSN protocol. MSN Messenger Protocol surely is an interesting site that should not be missed.
  2. Frankly, I've problems in getting the correct sequence to work under VB.Net, especially on the logon to the server. Until I encountered another project file on CodeProject site that demonstrate the MSN connection – <a href="">Connecting to MSN
  3. I've got that to work under VB.Net plus the MSN challenge handling

MD5 Hashing

After the MSN logon, it will receive the challenge key a short while later, we'll need to response back the key, by MD5 hashing the key, plus the original key, in order to keep online. Current version will wait until the challenge acknowledged by the MSN server, then multiple thread will fork for each of the online MSN ID, sending them message.

Function MD5Encrypt(ByVal sInput As String) As String

        Dim bytHashValue() As Byte

        Dim bytMsgValue() As Byte

        Dim Ue As ASCIIEncoding = New ASCIIEncoding

        Dim md5 As New MD5CryptoServiceProvider

        bytMsgValue = Ue.GetBytes(sInput)

        LogToFile("MD5 Key: " + sInput)

        bytHashValue = md5.ComputeHash(bytMsgValue)

        Dim sOutput As New StringBuilder

        Dim iloop As Integer

        For iloop = 0 To bytHashValue.Length – 1



        Return sOutput.ToString

End Function

Asynchronous with Multithread

This is the most challenging part. The code work correct as a single thread. After this point of calling the BeginRead, any next incoming data received via TcpClient will be response to the AsyncCallBack via SocketIncomingMsg. The EndRead will captur the incoming data in Byte stream.

readCallBack = New AsyncCallback(AddressOf SocketIncomingMsg)

tcpClient.GetStream.BeginRead(bytDataRead, 0, 1024,
      readCallBack, tcpClient.GetStream)

SendMSNCmd("SYN", "1")

     Private Sub SocketIncomingMsg(ByVal arMsg As IAsyncResult)

            Dim intCount As Integer

            intCount = tcpClient.GetStream.EndRead(arMsg)


     End Sub

When I initiated the asynchronous by just triggered the sub routine module (sample: btnTestCon click event). Everything turned out fine.


When I've called this via forking another thread to handle, it will fail to read the data from TCPClient, the EndRead will only return an error. Error such as “Unable to read data from the transport connection.” The same goes with the section that forked multiple threads, each for one online MSN ID. They all failed at the same point.

Dim MSNThread As Threading.Thread = New Threading.Thread(AddressOf Start_MSN)


I've attempt to catch the exception, and following the MSDN documentation, the inner exception too. But the message are not much helpful, (what is error 995 ??)

        Catch e As Exception


         Exception Output:

'The I/O operation has been aborted because of either 
'a thread exit or an application request
'at System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult)
'at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)

Many other attempts had I tried, but none successes. Finally I focus on the EndRead, does this have something to do with asynchronous under multithread environment? So I called the synchronous processing after the BeginRead.

readCallBack = New AsyncCallback(AddressOf SocketIncomingMsg)

Dim iarReturn As IAsyncResult = tcpClient.GetStream.BeginRead(bytDataRead, _
 0, 1024, readCallBack, tcpClient.GetStream)


It works!!!

Points of Interest

Comparing the previous version of VB, multithread programming under VB.Net is surely more stable, between the debugging and pausing, the development toolkit does not undergo sudden hang, where sometimes a reboot is needed.

But comparing the VC, there is more helpful class now, such as tcpclient, but then this also spelled another layer of re-learning, on top of the standard socket programming. Sometimes I really do not enjoy this. Development is made easy, but the base know-how of the tech is cover up – much like those days I started as VB developer, attempt with VC, the C/C++ old bird always making noise on the delivery, the programs are full of unnecessary lines, and no clean up of objects/class required, a habit that still follows me even now.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Luna Thong
Malaysia Malaysia
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionThanks a TON! Pin
Nisheeth Barthwal30-Jan-12 6:07
memberNisheeth Barthwal30-Jan-12 6:07 
GeneralSo Many Bugs Pin
csnetworks2-Jul-07 10:45
membercsnetworks2-Jul-07 10:45 
GeneralRe: So Many Bugs Pin
blackspotc29-Feb-08 18:22
memberblackspotc29-Feb-08 18:22 
GeneralRe: So Many Bugs Pin
RandomFish14-Feb-09 9:47
memberRandomFish14-Feb-09 9:47 
GeneralRe: So Many Bugs Pin
Mr_Jones22-Oct-09 17:46
memberMr_Jones22-Oct-09 17:46 
GeneralStart a BeginRead again when seeing SocketException 995 Pin
Orion Richardson224-Jul-06 3:23
memberOrion Richardson224-Jul-06 3:23 
GeneralNot working from XP Professional Pin
Suvendu Patra13-Jan-06 23:49
memberSuvendu Patra13-Jan-06 23:49 
GeneralRecording Transmited Voice Pin
triplebit4-Jan-06 7:50
membertriplebit4-Jan-06 7:50 
QuestionExcuse me? Pin
Csaba Fabian4-Nov-04 23:41
sussCsaba Fabian4-Nov-04 23:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 20 Jul 2004
Article Copyright 2004 by Luna Thong
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid