03 华为 harmonyos tcp 客户端 实现使用 模拟器亲测可行
华为 harmonyos tcp 客户端 使用 模拟器 亲测可行!!!
前言
为了实现鸿蒙模拟器TCP客户端 ,参考了官方的很多代码,发现问题特别多,使用模拟器又没办法开太多,用起来简直反人类,官方给的代码看起来又有点奇怪。
平时使用pyhont,c语音的tcp 客户端的时候,基本没见过bing操作,突然菊花有 bind 操作,使用官方的试了半天,又是第一次做,一直不成功。现在将网友的一份
代码修改,测试OK之后,并上传代码。
修订
onEnableClientClicked(){this.message="当前是TCP客户端"this.mode=1;// 创建一个TCPSocket连接,返回一个TCPSocket对象。this.tcpClient=socket.constructTCPSocketInstance();this.tcpClient.on('message',(value:SocketInfo)=>{console.log("on message");letbuffer=value.message;letdataView=newDataView(buffer);letstr="";for(leti=0;i<dataView.byteLength;++i){str+=String.fromCharCode(dataView.getUint8(i));}this.receivedMsg=str;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
测试
总结
无力吐槽,非常无奈,还能再相信菊花吗。
