alexstrat
alexstrat

Reputation: 111

Chrome UDP socket server sendTo other hosts than localhost returns -109 error code

I'm using the chrome network API to open an UDP socket that should both listen and send data (server and client). That's why i'm using the server way with bind/sendTo/recvFrom.

However, I am not able to send data to other hosts than local hosts: see my example below. The code error -109 corresponds to ADDRESS_UNREACHABLE.

chrome.socket.create('udp', function(socketInfo) {

  var socketId = socketInfo.socketId;

  chrome.socket.bind(socketId, '127.0.0.1', 3008, function(s) {

    chrome.socket.getInfo(socketId, function(info) {
      console.log(info);
      // {connected: false, localAddress: "127.0.0.1", localPort: 3008, socketType: "udp"} 
    });

    var arr = new Uint8Array(1);
    arr[0] = 123;
    var ab = arr.buffer;

    chrome.socket.sendTo(socketId, ab, "127.0.0.1", 3007, function(sendResult) {
      console.log("sendTo", sendResult);
      //sendTo {bytesWritten: 1}
    });
    chrome.socket.sendTo(socketId, ab, "google.com", 3007, function(sendResult) {
      console.log("sendTo", sendResult);
      //sendTo {bytesWritten: -109}
    });
  });
});

My permissions are the less restrictive ones: udp-send-to and udp-bind.

Using the connect/write way works but that's not what I want achieve, since i'm trying to open a socket than is server and client at same time.

Any idea ?

Upvotes: 1

Views: 1799

Answers (2)

alexstrat
alexstrat

Reputation: 111

I was able to make that run by binding the socket on 0.0.0.0 rather than 127.0.0.1.

chrome.socket.bind(socketId, '0.0.0.0', 3008, function(s) {
  //.. and so on
});

That was maybe what @EJP meant in his answer. I found the solution by browsing this thread.

Upvotes: 2

user207421
user207421

Reputation: 310957

If you bind your socket to 127.0.0.1 it can only send and receive to the localhost. So don't do that. Use the default.

Upvotes: 1

Related Questions