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

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
TABLE OF CONTENTS:
  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
SEARCH DEVARTICLES

Building A Document Request Protocol Part 1/2 - The VB application
(Page 3 of 7 )

To actually implement our protocol, we will create a Visual Basic application that will run on the server. Typically, a C, C++, or Perl application would be mapped to a port and execute whenever a connection request was attempted on that port, however to keep our article simple, we will use a VB6 app instead. Our application will run on the server, waiting for a connection request on port 2002.

Our VB app will contain just one form, as well as a reference to the Winsock component. It looks like this:

Our VB application

The form is composed of a frame, list box, command button, and a Winsock control. The list box will display the details of all data being sent to and captured from the client. The close button will end the program.

The Form_Load() event sets the Winsock controls protocol to TCP, and also sets its listening port to 2002:

Private Sub Form_Load()

wsSARP.Protocol = sckTCPProtocol

wsSARP.LocalPort = 2002

wsSARP.Listen

End Sub


When a connection request is detected, the ConnectionRequest event of the Winsock control is fired. It looks like this:

Private Sub wssarp_ConnectionRequest(ByVal requestID As Long)

wsSARP.Close

wsSARP.Accept requestID



End Sub


Once the connection request has been accepted, the client is notified and can begin to send commands to the server. Our Visual Basic application will only respond to commands that adhere to those defined for our protocol, SARP. Shown below is a list of commands (along with their details) that make up our SARP protocol:

LOGIN [user] [password]

The client is attempting to login to the server. Returns a status number, as well as a message, depending on whether or not the login was successful.

If the login was successful, then "102 Login OK" is returned. If it failed, then "103 Login Failed" will be returned.

LIST CATEGORIES

Returns a list of categories in the form of [category id] | [category name], with each new category separated by a semi-colon, for example:

1|BMW;2|Mercedes-Benz;3|Porsche

LIST ARTICLES [Category name]

Returns a list of articles in the form of [article id] | [title] | [content] | [price], with each new article separated by a semi-colon, for example:

1|BMW 318i|The 318i is the ideal family car. It's packed with style and sophistication, and comes in a wide range of colors and styles.|44000;

ADD CATEGORY [Category name]

Adds a new category to the categories table of our access database. If a category with [category name] already exists, then the category won't be added to the database.

Returns "104 Category Added OK" when a new category is successfully added, or "105 Category Already Exists" if the category already exists in the categories table.

ADD ARTICLE [Title] [Content] [Price] [Category Id]

Adds a new article to the articles table of our access database. If an article with [title] already exists, then the article won't be added to the database. Because each variable is separated by a space, all spaces in its title and content must be replaced with an underscore, like this:

ADD ARTICLE Carrera_Cabrolet This_is_a_nice_car 320000 3

Returns "106 Article Added OK" on success, or "107 Article Already Exists" if the article already exists in the articles table.

If any other command is received, then a blank line will be returned to the client indicating that the server didn't understand the command.

When a client sends a command to the server, our Winsock control grabs that command in its DataArrival event:

Private Sub wssarp_DataArrival(ByVal bytesTotal As Long)

Dim strData As String

Dim arrCommands() As String



wsSARP.GetData strData

lstDetails.AddItem "- Received Data """ & strData & """"



ProcessCommands strData

End Sub


The command is then passed onto the ProcessCommands routine, which breaks the command up and determines what the client is trying to do. Let's look at the ProcessCommands routine next.
blog comments powered by Disqus
PHP ARTICLES

- 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 
Support 

Developer Shed Affiliates

 




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