03 华为 harmonyos tcp 客户端 实现使用 模拟器亲测可行

发布时间:2026/6/3 4:24:29
03 华为 harmonyos tcp 客户端 实现使用 模拟器亲测可行
华为 harmonyos tcp 客户端 使用 模拟器 亲测可行!!!前言为了实现鸿蒙模拟器TCP客户端 参考了官方的很多代码发现问题特别多使用模拟器又没办法开太多用起来简直反人类官方给的代码看起来又有点奇怪。平时使用pyhontc语音的tcp 客户端的时候基本没见过bing操作突然菊花有 bind 操作使用官方的试了半天又是第一次做一直不成功。现在将网友的一份代码修改测试OK之后并上传代码。修订onEnableClientClicked(){this.message当前是TCP客户端this.mode1;// 创建一个TCPSocket连接返回一个TCPSocket对象。this.tcpClientsocket.constructTCPSocketInstance();this.tcpClient.on(message,(value:SocketInfo){console.log(on message);letbuffervalue.message;letdataViewnewDataView(buffer);letstr;for(leti0;idataView.byteLength;i){strString.fromCharCode(dataView.getUint8(i));}this.receivedMsgstr;console.log(on connect received:str);});this.tcpClient.on(connect,(){console.log(on connect);});this.tcpClient.on(close,(){console.log(on close);});// 绑定本地IP地址和端口。// let ipAddress: socket.NetAddress {} as socket.NetAddress;// ipAddress.address 127.0.0.1;// ipAddress.port 8866;// this.tcpClient.bind(ipAddress, (err: BusinessError) {// if (err) {// console.log(bind fail);// promptAction.showToast({ message: 绑定失败! JSON.stringify(err), duration: 5000 })// return;// }// console.log(bind success);// promptAction.showToast({ message: 绑定成功!, duration: 2000 })// });}//end onEnableClientClicked如上所示注释掉bind函数测试OK。https://gitee.com/fujianxinxi/tearcher.git测试总结无力吐槽非常无奈还能再相信菊花吗。