You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Linux下C开发OPC UA Server的DiscoveryUrl与EndpointUrl不匹配问题求助

解决Linux下OPC UA Server DiscoveryUrl与EndpointUrl不一致的问题

核心原因

Linux环境下,OPC UA SDK(如open62541)默认会将绑定到0.0.0.0的端点URL解析为网卡实际IP,而DiscoveryUrl则默认使用系统主机名,导致二者不一致,引发客户端连接验证失败。直接修改UA_ServerConfig结构体字段无效,是因为SDK初始化过程中会自动覆盖这些配置。

有效解决方案

1. 强制指定EndpointUrl模板

在服务器初始化前,修改端点URL模板,替换默认的{hostname}占位符为固定IP或主机名,确保EndpointUrl与DiscoveryUrl统一:

UA_ServerConfig *config = UA_ServerConfig_new_default();

// 替换模板为指定的IP或主机名
UA_String endpointTemplate = UA_String_fromChars("opc.tcp://192.168.1.100:{port}/");
UA_ServerConfig_setEndpointUrlTemplate(config, endpointTemplate);
UA_String_delete(&endpointTemplate);

// 后续创建服务器
UA_Server *server = UA_Server_new(config);

2. 自定义DiscoveryUrls并禁用自动填充

清空SDK自动生成的DiscoveryUrls,手动添加自定义地址,避免SDK启动时覆盖:

UA_ServerConfig *config = UA_ServerConfig_new_default();

// 清理自动生成的DiscoveryUrls
for(size_t i = 0; i < config->applicationDescription.discoveryUrlsSize; i++) {
    UA_String_delete(&config->applicationDescription.discoveryUrls[i]);
}
UA_free(config->applicationDescription.discoveryUrls);
config->applicationDescription.discoveryUrlsSize = 0;

// 添加自定义DiscoveryUrl
UA_String customDiscoveryUrl = UA_String_fromChars("opc.tcp://192.168.1.100:4840/");
config->applicationDescription.discoveryUrls = UA_realloc(config->applicationDescription.discoveryUrls, sizeof(UA_String));
config->applicationDescription.discoveryUrls[0] = customDiscoveryUrl;
config->applicationDescription.discoveryUrlsSize = 1;

3. 绑定到指定IP而非0.0.0.0

将服务器绑定到具体IP,避免SDK自动解析为网卡IP,确保EndpointUrl使用指定地址:

UA_ServerConfig *config = UA_ServerConfig_new_default();

// 指定绑定的IP地址
config->networkLayers[0].listenAddress = UA_String_fromChars("192.168.1.100");
// 关闭MDNS可减少自动解析干扰
config->networkLayers[0].mdnsConfig.mdnsEnabled = false;

关键注意事项

  • 所有配置修改必须在UA_Server_new(config)之前完成,服务器初始化后配置会被锁定,后续修改无效。
  • 若使用其他OPC UA SDK,配置API可能略有差异,但核心逻辑一致:在初始化阶段覆盖自动生成的URL规则。

内容的提问来源于stack exchange,提问作者Scravios

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 02:50:17