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:

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 SubWhen 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 SubOnce 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 CATEGORIESReturns 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|PorscheLIST 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 3Returns "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 SubThe 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.
Next: The ProcessCommands routine >>
More PHP Articles
More By Mitchell Harper