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.
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
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)
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.
Returns a list of categories in the form of [category id] | [category name], with each new category separated by a semi-colon, for example:
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.
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: