HarmonyOS TLSSocket发送消息报错

TLS socket单向认证。建立连接后发送数据控制台 [tls_socket.cpp 1195] write failed, return, err: 5错误。

建立连接代码如下:

public async connect() {
  if (this.isConnected) {
    return
  }

  let ipAddress : socket.NetAddress = {} as socket.NetAddress
  ipAddress.address = this.server
  ipAddress.port = this.port
  let options: socket.TLSConnectOptions = {
    address: {
      address: this.server,
      port: this.port,
      family: 1
    },
    secureOptions: {

    },
    ALPNProtocols: ["spdy/1", "http/1.1"]
  }

  let localAddress: socket.NetAddress = { address: "0.0.0.0", family: 1 }
  await this.socket.bind(localAddress)
  this.socket.on('message', async (value) => {
  })

  this.socket.connect(options, (err: BusinessError) => {
    if (err) {
      Logger.debug("socket connect failed: " + JSON.stringify(err), "socket")
      return
    }
    Logger.debug("socket did connect", "socket")
  })
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.

问题:

  1. bind方法是必须调用的吗,localAddress是本地真实IP,还是0.0.0.0就可以?
  2. 客户端单向认证服务端,只验证服务端证书是否系统根证书授信,TLSConnectOptions要如何传递呢,这种情况secureOptions传空可以吗?

有完整的tlssocket调用示例吗

HarmonyOS
2024-12-27 17:22:12
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
shlp

1、目前TLSSocket的客户端确实是需要bind,因为tls里面是在bind时候才创建连接。只有TLSSocket客户端必须bind,其他连接bind是可选的

2、本地IP使用0.0.0.0也可以

3、TLSConnectOptions传递方式可参考此链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-socket-V5#connect9

4、secureOptions不可以传空,具体内容可参考上方文档

分享
微博
QQ
微信
回复
2024-12-27 19:22:08
相关问题
HarmonyOS socket.TLSSocket发送Uint8Array对象
627浏览 • 1回复 待解决
HarmonyOS postman 发送推送消息失败
1348浏览 • 0回复 待解决
ets TCPSocket连接无法发送消息
4736浏览 • 1回复 待解决
webview如何进行消息发送以及接收?
1333浏览 • 1回复 待解决
HarmonyOS TLSSocket为什么强制需要bind
824浏览 • 1回复 待解决
基于TLSSocket的通信能力
1486浏览 • 1回复 待解决