|
To access the contents, click the chapter and section titles.
Advanced Visual Basic Techniques
(Publisher: John Wiley & Sons, Inc.)
Author(s): Rod Stephens
ISBN: 0471188816
Publication Date: 06/01/97
Part Four Client/Server Applications
In client/server applications one application, the server, provides some sort of service for another application, the client. The Add-Ins described in Chapters 4 through 7 are servers. They provide some sort of design service for the Visual Basic development environment. The Aligner application presented in Chapter 5, for example, provides control arranging services for the development environment.
Servers usually provide access to some sort of centralized or shared resource. The resource might be a physical device such as a scanner, printer, or special-purpose processor. It could also be a logical construct such as a database or executable code. Add-Ins are this kind of server. They provide centralized access to executable code that helps a programmer work in the development environment.
In the past, client/server programming has been fairly difficult. Making two applications communicate effectively can be tricky, particularly over a network. In some applications the interprocess communication programming makes the application-specific code seem trivial by comparison.
When Visual Basic 4 introduced objects, it made client/server programming much easier. By creating a class and exposing it to the world, an application can allow other programs to create instances of the class. The class objects become servers providing whatever methods they support as services to the program that created it. Note that this kind of server can only be built using 32-bit Visual Basic, not 16-bit Visual Basic.
The following chapters describe several different methods for creating client/server applications. The TimeSync project described in Chapter 11 allows one computer to display the system date and time used by another computer. It also allows the user to synchronize the client computers time with that of the server or to synchronize the servers time with that of the client. Even though this is a very simple application, it demonstrates the complex process of installing a remote server application.
Chapter 12 explains the QueryServer application that allows a user to select reports from remote report servers. By keeping report services centralized, QueryServer makes it easier to create and modify reporting capabilities.
The AsyncServer application described in Chapter 13 is similar to QueryServer, but it uses multiple servers to process requests for data asynchronously. The user can request several reports at the same time, and, if the reports are served by different computers, the servers can process the requests simultaneously.
Chapter 11 TimeSync
The PeopleWatcher Remote application described in Chapter 10 attaches remote database tables to a local database. This allows the program to treat the tables as if they were present on the local machine. It also allows many users on the same network to share data stored in a centralized database.
Not all programs, however, can use this database feature to provide centralized services. Many applications provide some service other than database access. To centralize these sorts of services requires true client/server programming.
The following section, Using TimeSync, describes the TimeSync application from the users point of view. It explains how a user can view a remote computers date and time and synchronize the times of two computers.
The Key Techniques section lists the application programming techniques used by the TimeSync application. The rest of the chapter describes those techniques in detail.
Using TimeSync
As time passes, the clocks of two computers tend to drift apart. If one system has a file system remotely mounted on another, this drift can cause confusion. Sometimes a file can be created on one computer so that it looks to the other as if the files creation time has not yet arrived. Synchronizing the two computers to within one or two seconds can prevent this sort of puzzling time travel.
The TimeSync client/server application allows a client application to access the system date and time on a server computer. The client can read the server systems time, synchronize its time to the servers time, or synchronize the servers time to its time.
Figure 11.1 shows TimeSync. When this image was taken, the TimeSync server application was running on the computer Beauty so the date and time shown in Figure 11.1 are the values for Beauty. The client program that produced the display shown in Figure 11.1 was running on the computer Beast.
Figure 11.1 The TimeSync application.
When the user clicks the Set Server Time button, TimeSync sets the servers time equal to the clients. In this example, the date and time on Beauty would be set equal to those on Beast.
If the user clicks the Set Client Time button, TimeSync sets the clients time equal to the servers. In this case, the values on Beast would be set equal to those on Beauty.
Key Techniques
Building and installing a remote server requires performing a complex series of tasks. Each task is relatively simple, but all must be performed correctly for the server to work.
At the same time, the application code must be debugged. Trying to debug an application served remotely on another machine while also trying to learn the server installation process can be quite confusing. Fortunately, there are ways to test and debug the server before it must be installed remotely.
Debugging an application is much easier if it is built in three distinct steps. As the steps progress, the server installation details become progressively more complicated. At the same time, the application code becomes more thoroughly tested. By the final step the server installation tasks are quite complex, but the application code has been thoroughly debugged.
The three steps and other programming techniques demonstrated by TimeSync are briefly described in the following list. The rest of Chapter 11 describes these techniques in detail.
- Step 1: Testing within the Client. Testing is easiest when the client and server are contained in the same application. This section describes how to test application code before adding the complexities of separate client and server programs.
- Step 2: Testing Locally. The next step is to test the client and server applications on the same computer. This section explains how to test the separate applications before the extra complexity of network connectivity is added.
- Step 3: Testing Remotely. Once the client and server are sufficiently debugged, they can be tested across a network. This section explains how to prepare a client and server for remote operation.
- Trouble Shooting. Solving problems with client/server applications can be quite difficult. This section lists some of the most common client/server problems and outlines their solutions.
- Using RegClass. The RegClass class provides functions for manipulating a client computers system registry. This section shows how a program can use RegClass objects to learn such things as the name of a server computer.
Step 1: Testing within the Client
Debugging a single, integrated program is much easier than debugging a system with clients and servers running on different computers. Before adding the complexities of network communication to an application, it is best to start with a client program that contains a server class object. The client and server are easier to debug when they are contained in the same program.
Figure 11.2 shows schematically how the client and server relate. Here the client application directly creates an instance of the server class. It then invokes the server objects methods to obtain service. The interaction between the client application and the server object is simple and direct, making it easy to debug. At this point, there are no complex remote communication issues, so they do not interfere with debugging the application itself.
TimeSync Step 1
In the integrated step 1 TimeSync program, the TimeSetter class is the server class. This simple class provides two public functions and two public subroutines. The GetDate and GetTime functions return the current system date and time, respectively. The SetDate and SetTime subroutines set the server systems date and time.
Public Function GetDate() As Date
GetDate = Date
End Function
Public Function GetTime() As Date
GetTime = Time
End Function
Figure 11.2 The server object running within the client application.
Public Sub SetDate(client_date As Date)
Date = client_date
End Sub
Public Sub SetTime(client_time As Date)
Time = client_time
End Sub
|