Skip to main content

Creating a Secure Connection to Key Services

To create a connection to an Pi Soft kS, use the TEConnection class as follows:

  1. Call the createInstance() method to create a smart pointer to a TEConnection object of SecureConnection type.
  2. Create a user authentication context and call the open() method of the TEConnection object to open a connection providing the server name, port number, and the context. For native user you can call the open() method of the TEConnection object with the server name, port number, username and password.
  3. Close it with the close() method when you have finished working with the connection.

The TEConnection object will be destroyed automatically when the smart pointer goes out of scope, so there is no need to delete it explicitly.


Code Samples

See the C++ environment settings for details on setting the project environment.


<cpp_admin_connect.cpp>

The following example illustrates how to create a secure connection to a Key Server with a native user

/*****************************************************************************************
*
* cpp_admin_connect.cpp
*

*
******************************************************************************************/

#include<string>
#include<iostream>

#include "teagent.h"
#include "teconnection.h"
#include "teexception.h"
#include "teauthuserctx.h"
#include "teauthwinctx.h"
#include "teobject.h"
#include "teadmin_local.h"

using namespace ERUCES;
using namespace std;

int main()
{
string strServer = "localhost";
long nPort = 8888;
string strAdminUser = "admin";
string strAdminPswd = "password1A";

try
{
auto_ptr<TEConnection> conn = TEConnection::createInstance(TEConnection::SecureConnection); // Use SecureConnection here

bool bUseContext = false;

// Connect to server
if( bUseContext )
{
// Open connection to the server for native user
auto_ptr<TEUserAuthenticationContext> ctx = TEUserAuthenticationContext::getInstance();
ctx->setUser(strAdminUser);
ctx->setPasswd(strAdminPswd);

conn->open(strServer, nPort, *ctx.operator->());
}
else
conn->open(strServer, nPort, strAdminUser, strAdminPswd);

cout << "Connected to " << strServer << " as " << strAdminUser
<< " at port " << nPort << endl;

// Disconnect from server
conn->close();

cout << "Disconnected from " << strServer << endl;
}
catch (TEException &e)
{
cout << e.getAllMessages().c_str() << endl;
}
catch (...)
{
cout << "Unexpected error" << endl;
}

return 0;
}
Output
Connected to localhost as admin at port 8888
Disconnected from localhost