主流程代码如下:
import { socket } from '@kit.NetworkKit'
const udp = socket.constructUDPSocketInstance() as socket.UDPSocket
const netAddress: socket.NetAddress = { address: '255.255.255.255', family: 1 }
let decodedMessage: string
const message = 'WloBEUgAkgCoAAAAFSklEQkHGBQAAAAAAAAAAAAAAAAAAAAAAAAAAH91vWs+T4t2LoScbleNZZAFN0CBc5DaiUZmYBtoGu69'
decodedMessage = buffer.from(message, 'base64').toString()
const sendOptions: socket.UDPSendOptions = {
data: decodedMessage as string | ArrayBuffer,
address: netAddress
}
udp.send(sendOptions, (err: BusinessError) => {
if (err) {
console.log('sendUDPMessage err')
return
}
console.log('sendUDPMessage success')
})
每次发送消息后,err处都返回Bad file descriptor,请问这个错误有没有具体的排查思路?
发送数据前需要先调用UDPSocket.bind()绑定IP地址和端口,参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-socket-V5\#send
默认不支持广播,参考示例设置: