Skip to content
This repository has been archived by the owner on Dec 29, 2022. It is now read-only.

receive udp packet data field is null #17

Open
shansjlin opened this issue Nov 1, 2016 · 2 comments
Open

receive udp packet data field is null #17

shansjlin opened this issue Nov 1, 2016 · 2 comments

Comments

@shansjlin
Copy link

shansjlin commented Nov 1, 2016

My code is shown as below, I create udp socket, set broadcast, bind receiveListener,and then send my request packet ipRequest to my udpServer:

receiveListener(info) {
let self = this;
console.log('wifiStore Recv from socket: ' + JSON.stringify(info));
}

startSearch() {
let self = this;
self.resetWifi();
if(window.chrome) {
  let ipRequest = [{msgType: 'ipRequest'}];
  let ipRequestStr = JSON.stringify(ipRequest);
  chrome.sockets.udp.onReceiveError.addListener(self.receiveErrorListener.bind(this));
  chrome.sockets.udp.onReceive.addListener(self.receiveListener.bind(self));
  chrome.sockets.udp.create(function(createInfo) {
    self._socketId = createInfo.socketId;
    console.log('startSearch createInfo.socketId:' + self._socketId);
    chrome.sockets.udp.bind(createInfo.socketId, '0.0.0.0', 0, function(result) {
      console.log('startSearch bind result:' + result);
      chrome.sockets.udp.setBroadcast(createInfo.socketId, true, function(result) { 
        console.log('startSearch setBroadcast result:' + result);
        
        self._intervalId = setInterval(function() {
          let message = self.stringToArrayBuffer(ipRequestStr);
          chrome.sockets.udp.send(createInfo.socketId, message, '255.255.255.255', 8081, function(result) {
            if (result < 0) {
              console.log('startSearch fail: ' + result);
            } else {
              console.log('startSearch success ' + result);
            }
          });
        } ,1000);

      });
    });
  });
} else {
  console.log('startSearch not window.chrome');
}

}
And my udpServer receive the request, as the log shown below:
serverLog - recv [{"msgType":"ipRequest"}](25 bytes) from client 192.168.1.112:63912 serverLog - recv [{"msgType":"ipRequest"}](25 bytes) from client 192.168.1.112:63912 serverLog - recv [{"msgType":"ipRequest"}](25 bytes) from client 192.168.1.112:63912

And the udpServer send the answer packet to my request client, the answer code is shown as below:

 var serverSocket = dgram.createSocket('udp4');
 serverSocket.on('message', function(msg, rinfo){
logger.warn('recv %s(%d bytes) from client %s:%d', msg, msg.length, rinfo.address, rinfo.port);
var msgStr = String(msg);
var message = JSON.parse(msgStr);
for (var i = 0; i < message.length; i++) {
  if (message[i].msgType && message[i].msgType === 'ipRequest') {
    if (ackFlag){
      var ip = getIPAdress();
      var ipAck = [{msgType: 'ipAck', IP: ip}];
      var msg = JSON.stringify(ipAck); 
      logger.debug('ack to client');
      serverSocket.send(msg, 0, msg.length, rinfo.port, rinfo.address);
    }
  }
}    

});

And my request client receive the answer packet, but the data is null, as shown below:
Recv from socket: {"socketId":0,"data":{},"remoteAddress":"192.168.1.125","remotePort":8081} Recv from socket: {"socketId":0,"data":{},"remoteAddress":"192.168.1.125","remotePort":8081}

the data field is null, but my expected data is "var ipAck = [{msgType: 'ipAck', IP: ip}];"

Anyone who can show me why? Thanks very much!!!

By the way, the request client is a cordova app, and is tested on IPad

@shansjlin shansjlin changed the title receive udp packet data is null receive udp packet data field is null Nov 1, 2016
@charliemciver
Copy link

Hopefully your UDP Server send function is working..... maybe used a UDP network monitor like PAcketSender to check....

In your receive event handler, I think your missed a step to get the data out of the info object and then reconvert it from a byte array to a string for the JSON handler.

receiveListener(info) {
let self = this;
data_string = self.arrayBuffertoString(info.data) //to be written - many examples out there.
console.log('wifiStore Recv from socket: ' + data_string);
}

@shansjlin
Copy link
Author

@charliemciver
Thanks very much. It works as you said.
Info.data need to be reconverted from arrayBuffer to string.
Thanks, you are so great.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants