When the sample server application was designed, its primary function was to service the needs of the client application. The application looks like a mirror of the client application, except that it cannot initiate a connection. Users will see their message and the reply in two separate text boxes. Rich Text Boxes were selected to correspond to the client application. The Rich Text Boxes were again designated Inbound and Outbound.
A text box was added to display the Remote Port number. This allows you to see that the Local Port on the client application and the Remote Port for the Server Application are the same.
Another text box was added to display the State Property of the Server. This changes from 2 (listening) to 7 (connected) as the application is run.
A final text box was added to automatically display the requestID sent by the Client Application or to be able to display the Local Port number for the server.
Of course, a Microsoft Winsock Control 5.0 is added to manage the communications between the client and server systems.
Laying Out the form
The Visual Basic form for the Server Application at runtime is shown in Figure 5.3. When you run the application, the Server Application is in a Listening State. It waits for a connection to be initiated by a client system.
There is no command button to initiate the connection on the Server Application form.
Adding the Code
In this section, you examine the code from the Server application. Since this is all of the code, notice the small amount of code that is required to create the functionality of the application. Adding error handling would at least double the total amount of code. Just as with the Client application, little code is required to use the power of the Winsock Control.
In Listing 5.8, a command button click event is used to display the Local Port number. Since this is set by the From Load event, it will read 1001 as set in Sub Form_Load() section.
Listing 5.8 - Sub cmdReadLocalPort_Click() - The Click Event that displays the Local Port number
Option Explicit Private Sub cmdReadLocalPort_Click() 'This reads and displays the LocalPort property. txtrequestID.Text = wskServer.LocalPort End Sub
The code in Listing 5.9 displays the remote port number. Display this number before and after the connection is made and you will see that the Remote Port number is set at the time the connection is made.
Listing 5.9 - Sub cmdReadRemotePort _Click() - The Click Event that displays the Remote Port number
Private Sub cmdReadRemotePort_Click() 'Read the RemotePort property and display in the txtRemotePort. txtRemotePort.Text = wskServer.RemotePort End Sub
Server state is an enumerated list. A server state of 2 is listening and 7 is connected. The code in Listing 5.10 displays the current server state.
Listing 5.10 - Sub cmdReadServerState_Click() - The Click Event that displays the Server State
Private Sub cmdReadServerState_Click() 'Read and Display the current Server State txtServerState.Text = wskServer.State End Sub
The Form Load event is being used to set the local port number for the Server Winsock Control and to use the Listen Method to place the Server application in a listening state as shown in Listing 5.11.
Listing 5.11 - Sub Form_Load() - The Form Load Event that is used to set various properties
Private Sub Form_Load() 'The name of the Winsock control for the server is wskServer ' This name is the object when setting a property such as ' object.property or wskServer.Property 'The LocalPort property can be set at design time or in code at runtime. ' The LocalPort property has a data type of Long Integer. ' This is port number on which the Server will be listening and must ' match the RemotePort property of the Client. wskServer.LocalPort = 1001 'The Listen method is used to start the server monitoring incoming ' requests for a connection. wskServer.Listen End SubListing 5.12 shows the Rich Text Box change event being used to send data to the client application.
Listing 5.12 - Sub rtbServOutBound_Change() - The Rish Text Box Change Event that is used to send data
Private Sub rtbServOutBound_Change() 'The RichTextBox_Change event is being used to send data. The ' contents of the RichTextBox rtbServOutBound will be ' transmitted each time the contents of the Text property is ' changed. The SendData method is used with the argument of ' RichTextBox.Text wskServer.SendData rtbServOutBound.Text End Sub
Listing 5.13 shows the Connection Request event being used to open the connection. The Accept method establishes the connection. The request ID is also displayed in a text box.
Listing 5.13 - Sub wskServer_ConnectionRequest(ByVal requestID As Long) - The Connection Request Event that is used to open the connection
Private Sub wskServer_ConnectionRequest(ByVal requestID As Long) 'Check to determine whether the WinSock Control's state is closed. ' If it is not, then close the control before using the Accept method. If wskServer.State <> sckClosed Then wskServer.Close 'The control is now prepared to use the Accept method which will ' receive the ConnectionRequest from the Client. ' The argument for the Accept method is requestID wskServer.Accept requestID 'The requestID is displayed txtrequestID.Text = requestID End Sub
Listing 5.14 uses the DataArrival event to display the data in a Rich Text Box.
Listing 5.14 - Sub wskServer_DataArrival(ByVal bytesTotal As Long) - The Data Arrival Event that is used to display the data
Private Sub wskServer_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. wskServer.GetData strData 'The strData variable contents are placed in the RichTextBox ' rtbClntInBound.Text property rtbServInBound.Text = strData End Sub
As with the code for the client application, the code required for the server Winsock Control is only a few lines. This is a truly powerful control.