Solved

VB Client/Server chat and queuing/stacking messages

Posted on 2003-11-09
3
239 Views
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.

Regards,
CyberSoft

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.
0
Comment
Question by:CyberSoft
  • 2
3 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
Comment Utility
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()
    Do
        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

Regards,

Idle_Mind
0
 
LVL 3

Author Comment

by:CyberSoft
Comment Utility
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.

Regards,
CyberSoft
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
Glad I could help.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now