Mastering PowerBuilder

HomePrevious Lesson: Course 3:: Session 23 :: Page 100
Next Lesson: Course 3:: Session 23 :: Page 120
Building the Server Application

In the server application, typically, you do three things.

Application Object’s Open event: Instantiate the transport object and start listening to the client requests by calling Listen() function. The following code starts listening to the client requests.

Transport gtr_transport
// Declare the above as the Global variable.
// The following code can go into the Open event.
gtr_transport = CREATE Transport
gtr_transport.Application = "PMSServer"
gtr_transport.Driver = "LocalHost"
gtr_transport.Listen()
If gtr_transport.ErrCode <> 0 THEN
   MessageBox( "Error in Starting the PMSServer", &
      String( ErrCode ) + ":" + gtr_transport.ErrText )
   Return
End If

Application Object’s ConnectBegin event: Write script to the ConnectBegin event to accept connect and assign connection privilage.

From the client application when you call ConnectToServer() function, the client application talks to the server application and triggers ConnectBegin event in the server. This event has three arguements, userid, password and ConnectString. The following code lists a typical coding.

// Script for the ConnectBegin event
String ls_pwd, ls_privilege
ls_pwd = ProfileString( &
         "server.ini", userid, "password", "" )
If userid <> ls_pwd Then
   Return NoConnectPrivilege!
End If
ls_privilege = ProfileString( "server.ini", userid, &
                "privilege", "None" )
If upper( ls_privilege ) = "ADMIN" Then
   w_console.of_StatusDisplay( userid, "Administrator" )
   Return ConnectWithAdminPrivilege!
ElseIf upper( ls_privilege ) = "CONNECT" Then
   w_console.of_StatusDisplay( userid, "Normal Connection" )
   Return ConnectPrivilege!
ElseIf upper( ls_privilege ) = "NONE" Then
   w_console.of_StatusDisplay( userid, "Refused" )
   Return NoConnectPrivilege!
End If

In the above code, we are reading the password from the "server.ini" file. If the password doesn’t match, we are refusing the connection by returning the NoConnectPrivilege! enumerated return code. Otherwise, we are granting the connection privilege depending on the settings in the "server.ini" profile file. We are also writing to the console program about the connection status.

Application Object’s ConnectEnd event: In this event, typically, you write to the console saying, the client connection is closed and disconnect from the database also.

HomePrevious Lesson: Course 3:: Session 23 :: Page 100
Next Lesson: Course 3:: Session 23 :: Page 120