---
title: 建链
description: "调用Connect接口向Server侧建立通信链路，链路主要用于数据的传输，其中P/D均可作为Server或者Client，按需设置。"
url: https://www.hiascend.com/document/detail/zh/canncommercial/latest/API/hixlug/llmcdv2_42_27.html
sourcePath: /source/zh/canncommercial/900/API/hixlug/llmcdv2_42_27.html
indexId: 1a24e555add463af87759d32dee663e3a9131ad94e1536da0e9541177c5784df59
---
# 建链

#### 功能介绍

调用Connect接口向Server侧建立通信链路，链路主要用于数据的传输，其中P/D均可作为Server或者Client，按需设置。


#### 使用场景

建链操作是节点之间进行数据传输的前提，建链接口采用类TCP建链流程，Server初始化时提供侦听信息，由Client侧发起建链。


#### 功能示例

初始化HIXL。其中Server侧需要设置侦听的Host IP和port。

```
// Server侧
Hixl engine;
std::map<AscendString, AscendString> options;
AscendString local_engine = "ip:port"; // 替换成真实IP端口；是HIXL的唯一标识，端口为有效值
auto ret = engine.Initialize(local_engine, options);
if (ret != SUCCESS) {
    printf("[ERROR] Initialize failed, ret = %u\n", ret);
    return -1;
}
// Client侧
Hixl engine;
std::map<AscendString, AscendString> options;
AscendString local_engine = "ip:port"; // 替换成真实IP；是Hixl的唯一标识，端口可不设置或设置为负数，此时表示不侦听
auto ret = engine.Initialize(local_engine, options);
if (ret != SUCCESS) {
    printf("[ERROR] Initialize failed, ret = %u\n", ret);
    return -1;
}
```


在Client侧调用Connect发起建链操作。

```
AscendString remote_engine = "ip:port"; // 替换成需要与之建链的Server的local engine
auto ret = engine.Connect(remote_engine);
if (ret != SUCCESS) {
    printf("[ERROR] Connect failed, ret = %u\n", ret);
    return -1;
}
```


#### 异常处理

当调用建链失败时，需排查两台机器网络是否连通，Host IP是否正确，port是否被占用。

更多异常处理请参考HIXL错误码。
