Connecting to a server
To allow a client application
to function in a distributed environment, you need to connect to
a server application. Connecting to a server application is very similar
to connecting to a database. To establish the connection, you use
the Connection object, a nonvisual object
that works like the Transaction object.
Version 5 clients cannot connect to Version 6 or 7
servers Because of architectural changes made in PowerBuilder Version
6, client applications written in Version 5 cannot connect to server
applications written in Version 6 or 7.
Declaring the connection variable
Unlike
the Transaction object, the Connection object is not a built-in
global object. You need to declare a variable of type connection.
Typically, you will want this to be a global or instance variable.
Establishing a connection
To
establish a connection to a server application, you need to execute
the PowerScript statements required to perform these operations:
- Use the Create statement
to instantiate the Connection object. - Set properties for the Connection object.
- Invoke the ConnectToServer function to establish
a connection to the server application. - (Optional) Check for errors.
You can perform these operations in a single script or in
several scripts, but they must be performed in the order shown above.
Example The following script instantiates the myconnect Connection
object and sets the connection properties to identify the communications
driver, the server location, and the name of the server application.
Then the script invokes the ConnectToServer function to establish
a connection to the server and checks for errors. This example uses
the WinSock driver:
1 |
// Global variable:// connection myconnect |
1 |
1 |
long ll_rc |
1 |
myconnect = create connection |
1 |
myconnect.driver = "WinSock" |
1 |
myconnect.application = "dpbserv" |
1 |
myconnect.location = "server01" |
1 |
ll_rc = myconnect.ConnectToServer() |
1 |
IF ll_rc <> 0 THEN |
1 |
MessageBox("Connection failed", ll_rc) |
1 |
END IF |
WinSock name resolution
To tell PowerBuilder how to connect to the server application,
you specify values for the Location and Application properties of
the Connection object. When you use the WinSock driver, you can
specify the IP address and port number for the server directly,
or provide a service name and a host name. If you specify a service
name and/or host name, PowerBuilder uses your TCP/IP network
settings to locate the server application. To determine the IP address and
port number for the server, PowerBuilder will use standard name resolution
procedures such as DNS, WINS, and the hosts and services files.
Updating the hosts and services files
If you use the hosts and services files for name resolution,
you need to be sure to use the correct format for the entries you
add. For example, to identify the dpbserv service, you could include
an entry like the following in the services file:
1 |
dpbserv 10015/tcp |
Line must be terminated with a carriage return or linefeed If you add an entry to the end of the services file, you need
to press return after the final entry. Otherwise,
the final entry will not be recognized.
To identify the server01 host, you could include an entry
like the following in the hosts file:
1 |
128.66.12.1 server01 |
The location of the services and hosts files depends on which
TCP/IP implementation and operating system you use.
Establishing multiple connections
PowerBuilder allows you to instantiate multiple Connection
objects. This makes it possible for you to establish multiple connections
in a single client application. For example, you could instantiate
two separate Connection objects to connect a client to two different
server applications. The connections could be established by using
the same or different communications drivers.