Home arrow PHP arrow Page 4 - Building A Document Request Protocol Part 1/2

Building A Document Request Protocol Part 1/2

Disparate systems communicate with each other using protocols. A protocol is a set of rules that governs the transmission of data between two independent systems. If you're familiar with protocols, then you will know that most are composed of simple, plain text commands. In this article Mitchell shows us how to create a protocol and implement it as a server using Visual Basic and Winsock.

Author Info:
By: Mitchell Harper
Rating: 5 stars5 stars5 stars5 stars5 stars / 2
February 23, 2002
  1. · Building A Document Request Protocol Part 1/2
  2. · Our protocol: SARP
  3. · The VB application
  4. · The ProcessCommands routine
  5. · Accepting new records from the client
  6. · Our VB App in action
  7. · Conclusion

print this article

Building A Document Request Protocol Part 1/2 - The ProcessCommands routine
(Page 4 of 7 )

The great thing about our Visual Basic application is that it doesn't matter what type of client is passing the requests to it; as long as the client sends the commands in our pre-defined SARP format, then our Visual Basic app (in combination with our Winsock control) will return the data that the client's after.

The ProcessCommands function processes the data received from the client. It is the main loop of our entire VB application. Its signature looks like this:

Private Sub ProcessCommands(ByVal strCmds As String)

As you can guess, the strCmds argument is the command captured from the client by the Winsock control. It is split into an array, like this:

Dim arrCmds() As String

arrCmds = Split(strCmds, " ")

From here, we can use a select case command to determine what type of command the client wants the results for:

Select Case UCase(arrCmds(0))

Case "LOGIN"


Case "LIST"


Case "ADD"


Case Else


If the user is trying to login, then the array will contain three indexes: "LOGIN", their user id, and their password. These details are passed to the DoLogin() function, which sets the value of the global Boolean variable blnLoggedIn to true if the users credentials are valid. In our example I've simply hard coded the values, but you could just as easily add a table to our Access database:

Private Function DoLogin(ByVal strUser As String, ByVal strPass As String)

If UCase(strUser) = "XUSER" And UCase(strPass) = "XPASS" Then

DoLogin = "102 Login OK"

blnLoggedIn = True


DoLogin = "103 Login Failed"

blnLoggedIn = False

End If

End Function

You'll notice that throughout our VB app, most of the functions return a message in the form of [status] [message text]. This is a common message format among protocols, and comes in handy when the client needs only a status number, or only a status message.

From this point onwards, the user must be logged in to execute any of our other SARP commands. If they are not, then they receive a message telling them so:

wsSARP.SendData "-1 You are not logged in" & vbCrLf

lstDetails.AddItem "- Sent Data ""-1 You are not logged in """

When our select case statement comes across a "LIST" command, it does some array value checking to determine whether the client wants a list of categories or articles:

Case "LIST"

If blnLoggedIn Then

'Is the user requesting groups or articles?

If UBound(arrCmds) >= 1 Then

If arrCmds(1) = "CATEGORIES" Then

strResult = GetCategories()

wsSARP.SendData strResult & vbCrLf

lstDetails.AddItem "- Sent Data """ & strResult & """"

ElseIf arrCmds(1) = "ARTICLES" Then

If UBound(arrCmds) = 2 Then

strResult = GetArticles(CInt(arrCmds(2)))

wsSARP.SendData strResult & vbCrLf

lstDetails.AddItem "- Sent Data """ & strResult & """"

The GetCategories function retrieves a list of categories from the database and returns them as a single separated-value string:

Set objRS = GetRecordset("SELECT * FROM Categories ORDER BY name ASC")

While Not objRS.EOF

strCats = strCats & objRS("id") & "|" & objRS("name") & ";"



GetCategories = strCats

The GetRecordset function is a custom function that accepts an SQL query. It returns the results of this query as an ADO recordset. We loop through the results, create the strCats variable, and then return it.

The GetRecordset function uses the constant string variable CONNECTION_STRING, which is defined under the global declarations section. Our connection string references a system DSN that points to our Access database.

If the clients SARP command is LIST ARTICLES [Category Id], then the GetArticles function is called. It returns the complete details of each article whose catId field matches the category Id passed in by the client:

Private Function GetArticles(intCatId As Integer) As String


Set objRS = GetRecordset("SELECT * FROM Articles WHERE catId = " & intCatId & " ORDER BY title ASC")

While Not objRS.EOF

strCats = strCats & objRS("id") & "|" & objRS("title") & "|" & objRS("content") & "|" & objRS("price") & ";"



GetArticles = strCats

End Function

blog comments powered by Disqus

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials