Now that your application can determine the appropriate RPC Broker server and port to connect to, you can add code to establish a connection to the designated RPC Broker server from your application. The act of establishing a connection leads the user through signon. If signon succeeds, a connection is established.
To establish a connection from your application to a RPC Broker server:
a. Set RPCBroker1's Connected property to True (inside of an exception handler try...except block). This will cause an attempt to connect to the RPC Broker server.
b. Check if an EBrokerError exception is raised. If this happens, connection failed, and your code should inform the user of this and terminate the application.
The OnCreate event handler should now look like:
procedure TForm1.FormCreate(Sender: TObject);
var ServerStr: String;
PortStr: String;
begin
// get the correct port and server from Registry
if GetServerInfo(ServerStr,PortStr)<> mrCancel then
begin {connectOK}
RPCBroker1.Server:=ServerStr;
RPCBroker1.ListenerPort:=StrToInt(PortStr);
// establish a connection to the RPC Broker server
try
RPCBroker1.Connected:=True;
except
On EBrokerError do
begin {error}
ShowMessage('Connection to server could not be established!');
Application.Terminate;
end; {error}
end; {try}
end {connectOK}
else
Application.Terminate;
end;
Note Every call that invokes an RPC Broker server connection should be done in an "exception handler" try...except block, so that EBrokerError exceptions can be trapped.
Now that your application can establish a connection to the end-user's server system, you can go about retrieving data from the server system. In the next steps of the tutorial, you will create a custom RPC that retrieves a list of all of the terminal types on the server system, and call that RPC from your application.