VB Client/Server chat and queuing/stacking messages

Posted on 2003-11-09
Medium Priority
Last Modified: 2010-05-01
Hi there,

I'm busy writing a client/server chat application in VB6 (I know re-inventing the wheel here but I want to see how far I can get with this without having to use third-party components, apps, etc.).

I've got the basics working pretty well - one application is acting as the server (accepting, sending and relaying messages between client applications. These messages can be normal messages between users or various commands sent by the client/server (refresh user list, change nickname, connect, disconnect, etc).

As I said all works pretty well - I can see when users are connected, send and receive messages. But only if there's a few connected clients and only if not more than one message comes into the server or client application at the same time.

What I'm interested in the best way to implement some sort of messaging queue or stack for the messages (received and sent) so that the client/server application can parse and process each message separately and not bomb-out/ignore messages that contain more than one command in them.

I hope my question is clear enough - let me know if you need more information.

Thanks in advance.


PS: I will check/respond as and when I get a chance over the next 2 days - afte that I'm on vacation for a few days (till Sunday) so I won't be able to respond immediately to comments/posts.
Question by:CyberSoft
  • 2
LVL 86

Accepted Solution

Mike Tomlinson earned 1000 total points
ID: 9711097
Just add some kind of end of message marker to your messages.  Many people use vbCrLF.

Then, whenever your client/server receives data, just add whatever it may be to the end of a string buffer.

Next, check your string buffer for the presence of a vbCrLf using the InStr function.  If there is one, then extract the first command from the buffer using the Left command up to the marker and reset the buffer to whatever is left after the marker.  Do this in a loop and you will process all (if any) commands you receive.

I don't know how you communicating between your client/server so here is some generic code that can be modified easily:

Private Sub whatEverEventTriggersWhenYouReceiveData()
    Static bufferData As String
    Dim markerposition As Integer
    Dim currentMessage As String
    bufferData = bufferData & whatEverFunctionYouUseToGetBufferData()
        markerPos = InStr(bufferData, vbCrLf)
        If markerPos > 0 Then ' Found a marker
            ' Extract complete message from buffer
            currentMessage = Left$(bufferData, markerPos - 1) ' leave off vbCrLf

            'now do something with that message
            yourMessageProcessingSub currentMessage
            ' Reset buffer to what's after the marker
            bufferData = Mid$(bufferData, markerPos + 2) ' leaves out vbCrLf
        End If
    Loop While markerPos > 0 ' go back and process all complete messages in buffer
End Sub



Author Comment

ID: 9723714
Hi Idle_Mind,

Sorry for the delay in getting back to you. You are absolutely right. I don't know why I wasn't thinking simpler - I was thinking more along the lines of arrays and timers and pushing messages into an array ... blah blah blah.

When the simple answer was right there (I've even used it elsewhere in my app to split separate parts of a message, eg: message and userid and recipientid)

Well you came up with a solution that works so the points are yours well done.

LVL 86

Expert Comment

by:Mike Tomlinson
ID: 9725135
Glad I could help.

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

586 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question