PHP
  Home arrow PHP arrow Page 4 - Building A Document Request Protocol Part ...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Building A Document Request Protocol Part 1/2
By: Mitchell Harper
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2002-02-23

    Table of Contents:
  • Building A Document Request Protocol Part 1/2
  • Our protocol: SARP
  • The VB application
  • The ProcessCommands routine
  • Accepting new records from the client
  • Our VB App in action
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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

    Else

    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") & ";"

    objRS.MoveNext

    Wend



    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") & ";"

    objRS.MoveNext

    Wend



    GetArticles = strCats

    End Function

    More PHP Articles
    More By Mitchell Harper


     

    PHP ARTICLES

    - 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
    - PHP Frontend to ImageMagick
    - Using PEAR's mimeDecode Module
    - Incoming Mail and PHP






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT