I modified UaSDKCpp client example to connect to OPC Foundation's UA Sample Server which is running on another machine.
Supporse we only know server URL, so we call UaDiscovery's getEndpoint() function to get the server's endpoint descriptions first.
Then we choose a EndpointUrl from one of the endpoint descriptions. In the end, we call UaSession's connect() funtion to connect to server specified by the EndpointUrl.
I failed at connect() funtion, because the EndpointUrl we get from endpoint description is "opc.tcp://localhost:51210/UA/SampleServer".
As the server is running on another machine, we cann't access it through "localhost".
So, what is the correct way to connect to server? Using server URL instead of EndpointUrl?
Here is my code, it's based on UaSDKCpp client example lesson 1: http://documentation.unified-automation ... son01.html
Code: Select all
UaStatus SampleClient::connect()
{
UaStatus result;
UaString serverUrl("opc.tcp://vm-27714:51210/UA/SampleServer"); //new code
// Provide information about the client
SessionConnectInfo sessionConnectInfo;
UaString sNodeName("unknown_host");
char szHostName[256];
if (0 == UA_GetHostname(szHostName, 256))
{
sNodeName = szHostName;
}
sessionConnectInfo.sApplicationName = "Unified Automation Getting Started Client";
// Use the host name to generate a unique application URI
sessionConnectInfo.sApplicationUri = UaString("urn:%1:UnifiedAutomation:GettingStartedClient").arg(sNodeName);
sessionConnectInfo.sProductUri = "urn:UnifiedAutomation:GettingStartedClient";
sessionConnectInfo.sSessionName = sessionConnectInfo.sApplicationUri;
// new code begin
ServiceSettings serviceSettings;
ClientSecurityInfo clientSecurityInfo;
UaEndpointDescriptions endpointDescriptions;
UaDiscovery discovery;
result = discovery.getEndpoints(
serviceSettings,
serverUrl,
clientSecurityInfo,
endpointDescriptions);
if (result.isNotGood())
{
return result;
}
OpcUa_UInt32 endpointIndex = 0;
for (; endpointIndex < endpointDescriptions.length(); ++endpointIndex)
{
if (endpointDescriptions[endpointIndex].SecurityMode == OpcUa_MessageSecurityMode_None)
{
if (UaString(endpointDescriptions[endpointIndex].SecurityPolicyUri) == OpcUa_SecurityPolicy_None)
{
break;
}
}
}
if (endpointIndex == endpointDescriptions.length())
{
return OpcUa_Bad;
}
//new code end
// Security settings are not initialized - we connect without security for now
SessionSecurityInfo sessionSecurityInfo;
printf("\nConnecting to %s\n", UaString(endpointDescriptions[endpointIndex].EndpointUrl).toUtf8());
result = m_pSession->connect(
endpointDescriptions[endpointIndex].EndpointUrl, //new code
sessionConnectInfo,
sessionSecurityInfo,
this);
if (result.isGood())
{
printf("Connect succeeded\n");
}
else
{
printf("Connect failed with status %s\n", result.toString().toUtf8());
}
return result;
}
Thanks!