When the sample client application was designed, the decision was made that users would want to see their message and the reply in two separate test boxes. Rich Text Boxes were selected for the ability to use text formatting. The Rich Text Boxes were designated Inbound and Outbound.
A command button was added to be used to initiate the connection.
Two text boxes were added to enter the Remote Host name or IP and the Remote Port. This allows the host and port to be selected and set at runtime.
A final text box was added to display the local port number. This local port number can be read before the connection is created and after the connection is made.
Finally, a Microsoft Winsock Control 5.0. This Winsock Control will manage the communications between the client and server systems.
Laying Out the Form
The Visual Basic form for the Client Application at design time is shown in Figure 5.1. When the application is run, the Remote Host name or IP address needs to be entered and the Remote Port number needs to be entered before the Connect button is clicked. Once the connection is established, text may be entered in the Outbound Text box. When entered, it is automatically dispatched to the Server application.
Notice that the Winsock Control is visible at design time.
In Figure 5.2, the appearance of the form at runtime is the same as the design time appearance with the exception that the Winsock Control is not visible.
The Remote Host and Remote Port setting must be entered before the connection is attempted in this example program.
Adding the code
In this section, you examine the code from the client application just as it is in the program. Notice the small amount of code required to create the functionality of the application. The addition of error handling would more than double the code. Here you can see the power of the Winsock Control. In a few lines of code, you accomplish full TCP/IP communication.
In Listing 5.1, a command button is used to initiate the connection. The Connect method is used. This method gathers the current values in two properties, the RemoteHost property and the RemotePort property, and creates the TCP message that will contact the Remote Host and request the connection. By setting two properties and calling one method, you have accomplished what can require hours or even days of programming to accomplish by using the Winsock API.
Listing 5.1 - Sub cmdConnect_Click() - The Click Event that creates the connection
Option Explicit Private Sub cmdConnect_Click() 'The command button click event is used to create the connection. ' The connection request string is sent to the RemoteHost on the RemotePort. ' The Connect method takes two arguements, in the form of ' object.Connect remoteHost, remotePort ' If the two properties are blank and the arguments are not supplied an error ' occurs. The two properties were set in the Form_Load event which occurs ' prior to the Command_Click event. wskClient.Connect End Sub
Listing 5.2 shows the sub provided to allow you to see that the Local Port is set to 0 before the connection is made and will be set to a value selected by the system in the process of making the connection.
Listing 5.2 - Sub cmdReadLocalPort_Click() - The Click Event that displays the local port number
Private Sub cmdReadLocalPort_Click() 'This will read and display the value of the LocalPort property in ' txtLocalPort.Text txtLocalPort.Text = wskClient.LocalPort End Sub
Listing 5.3 shows the sub that allows the RemoteHost property to be set at runtime. If the client is always going to communicate with the same server, the RemoteHost property can be set at design time.
Listing 5.3 - Sub cmdSetRemoteHost_Click() - The Click Event that sets the RemoteHost property
Private Sub cmdSetRemoteHost_Click() 'This will set the value of the RemoteHost property to the value in ' txtRemoteHost.Text wskClient.RemoteHost = txtRemoteHost.Text End Sub
Listing 5.4 shows the sub that allows the RemotePort property to be set at runtime. If the client is always going to communicate with the same port number, the RemotePort property can be set at design time.
Listing 5.4 - Sub cmdSetRemotePort_Click() - The Click Event that sets the RemotePort property
Private Sub cmdSetRemotePort_Click() 'This will set the value of the RemotePort property to the value in ' txtRemotePort.Text wskClient.RemotePort = txtRemotePort.Text End Sub
Listing 5.5 shows another method of setting the RemoteHost and RemotePort properties in code.
Listing 5.5 - Sub Form_Load() - The Form Load event can be used to perform initiation tasks
Private Sub Form_Load() 'The name of the Winsock control for the client is wskClient ' This name is the object when setting a property such as ' object.property or wskClient.Property 'The name of the remote host can be set at design time in the ' properties dialog or it can be set at runtime in code. ' Either the IP address "XXX.XXX.XXX.XXX" or a host friendly ' name may be used such as "http://www.microsoft.com" ' The RemoteHost property has a data type of string. wskClient.RemoteHost = "SomeJunk" 'The RemotePort property can be set at design time or in code at runtime. ' The RemotePort property has a data type of Long Integer. ' This is port number on which the Server will be listening. wskClient.RemotePort = 1002 End Sub
Using the Change event to send the entire contents of a Rich Text Box, as shown in Listing 5.6 is a technique that is quite inefficient since that entire message is retransmitted with each change in the contents. It does illustrate the simplicity of sending data with the SendData method.
Listing 5.6 - Sub rtbClntOutBound_Change() - The Change Event is used to send data
Private Sub rtbClntOutBound_Change() 'The RichTextBox_Change event is being used to send data. The ' contents of the RichTextBox rtbClntOutBound will be transmitted ' each time the contents of the Text property is changed. ' The SendData method is used with the argument of RichTextBox.Text wskClient.SendData rtbClntOutBound.Text End Sub
Listing 5.7, is a two-step process of moving the data from the buffer into a variable and then placing the variable contents into the Text Property of the Rich Text Box. It can be placed directly into the Text property of the Rich Text Box.
Listing 5.7 - Sub wskClient_DataArrival(ByVal bytesTotal As Long) - The data arrival event is used to display the data in a text box
Private Sub wskClient_DataArrival(ByVal bytesTotal As Long) 'The variable strData is declared to hold the incoming data. ' It is stored as a variant. Dim strData As String 'The GetData method takes the data from the incoming buffer ' and places it in the strData variable. wskClient.GetData strData 'The strData variable contents are placed in the RichTextBox ' rtbClntInBound.Text property rtbClntInBound.Text = strData End Sub
The salient feature of the client application program code is its simplicity. You can see that the assertion made at the beginning of the chapter, that if you can write a "Hello World Application," you can use the Winsock Control was not an exaggeration.