13.2.1. Opening and Closing a Connection

As described above, the first thing an application must do is open a connection to the server. When doing this, the application must provide the address (host and port) of the server where EKM is running, the username, and password of the user logging into EKM. Some of the samples are shown using localhost. This will not work since the EKM server does not accept connections on the loopback address. All scripts must specify the actual name of the EKM server.

After the connection is opened, the application can make use of the library’s functionality to access and manage data on the server. The one thing the application should always do when exiting is close the connection. This will ensure that a license acquired by the user is released.

The following examples show how to open and close a connection:

Java
package samples.java;

import java.net.InetAddress;

import com.ansys.ingress.client.api.Connection;
import com.ansys.ingress.client.api.ConnectionFactory;

public class OpenCloseConnection {

    public static void main(String[] args) throws Exception {
        Connection conn = null;

        try {
            // Edit the following lines as needed for your installation
            conn = ConnectionFactory.open(
                // Host URL
                "http://" + InetAddress.getLocalHost().getHostName() + ":8080",
                // User ID
                "ekmadmin",
                // Password
                "Adminuser123!"
            );
            System.out.println(">>>>> Connection " + conn + " opened");
        } finally {
            if (conn != null) {
                conn.close();
            }
            System.out.println(">>>>> Connection " + conn  + " closed");
        }
    }
}
Python (Jython)
from com.ansys.ingress.client.api import ConnectionFactory

import socket

if __name__ == "__main__":
    """
    Simple example of opening and closing a connection on the server.
    """
    conn = None
    try:
        host = "http://" + socket.gethostname() + ":8080"
        conn = ConnectionFactory.open(
            host,                    # host
            "ekmadmin",              # username
            "Adminuser123!")         # password
        print ">>>>> Connection " + str(conn.getName()) + " opened"
    finally:
        if conn:
            conn.close()
            print ">>>>> Connection " + str(conn.getName()) + " closed"
Python (IronPython)
# Load EKM assemblies
import client_init

from com.ansys.ingress.client.api import ConnectionFactory

if __name__ == "__main__":
    conn = None
    try:
        # Change the username and password below as needed
        conn = ConnectionFactory.Open(
            "http://ekmserveraddr:8080", # host
            "ekmadmin",                  # username
            "Adminuser123!")             # password
        print ">>>>> Connection " + conn.GetName() + " opened"
    finally:
        if conn:
            conn.Close()
            print ">>>>> Connection " + conn.GetName() + " closed"
C#
Connection conn = null;

    try
    {
        //
        // Modify the workspace, user name, and password to reflect
        // the actual EKM server you are using.
        ///
        conn = ConnectionFactory.Open(
            "http://ekmserveraddr:8080", // Host URL
            "ekmadmin",                  // EKM user
            "Adminuser123!"              // User's password
        );
        Console.WriteLine(">>>> Connection " + conn.GetName() + " opened");
    }
    finally
    {
        if (conn != null)
        {
            conn.Close();
            Console.WriteLine(">>>> Connection " + conn.GetName() + " closed");
        }
    }
}

Release 18.2 - © ANSYS, Inc. All rights reserved.