Click here to Skip to main content
15,886,597 members
Articles / Programming Languages / Visual Basic
Article

A TCP client that uses MSN protocol

Rate me:
Please Sign up or sign in to vote.
3.09/5 (8 votes)
19 Jul 20043 min read 108.1K   560   40   9
A TCP client that uses MSN protocol

Image 1

Introduction

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="http://www.codeproject.com/csharp/ConnectingToMSN.asp">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.

VB.NET
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

            sOutput.Append(bytHashValue(iloop).ToString("X2"))

        Next

        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.

VB.NET
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.

VB.NET
Start_MSN()

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.

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

MSNThread.Start()

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 ??)

VB.NET
        Catch e As Exception

               LogToFile(e.InnerException.ToString())
 

         Exception Output:

'System.Net.Sockets.SocketException: 
'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)
'995

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.

VB.NET
readCallBack = New AsyncCallback(AddressOf SocketIncomingMsg)

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

iarReturn.AsyncWaitHandle.WaitOne()

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.

License

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


Written By
Malaysia Malaysia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionThanks a TON! Pin
Nisheeth Barthwal30-Jan-12 6:07
Nisheeth Barthwal30-Jan-12 6:07 
GeneralSo Many Bugs Pin
csnetworks2-Jul-07 10:45
csnetworks2-Jul-07 10:45 
GeneralRe: So Many Bugs Pin
blackspotc29-Feb-08 18:22
blackspotc29-Feb-08 18:22 
GeneralRe: So Many Bugs Pin
RandomFish14-Feb-09 9:47
RandomFish14-Feb-09 9:47 
GeneralRe: So Many Bugs Pin
Mr_Jones22-Oct-09 17:46
Mr_Jones22-Oct-09 17:46 
GeneralStart a BeginRead again when seeing SocketException 995 Pin
Orion Richardson224-Jul-06 3:23
Orion Richardson224-Jul-06 3:23 
GeneralNot working from XP Professional Pin
Suvendu Patra13-Jan-06 23:49
Suvendu Patra13-Jan-06 23:49 
GeneralRecording Transmited Voice Pin
triplebit4-Jan-06 7:50
triplebit4-Jan-06 7:50 
QuestionExcuse me? Pin
Csaba Fábián4-Nov-04 23:41
professionalCsaba Fábián4-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.