Chrome packaged app UDP sockets not working-Collection of common programming errors

The Network Communications doc is not up-to-date. See the latest API doc: https://developer.chrome.com/trunk/apps/socket.html. But the doc doesn’t state everything clearly. I looked into Chromium source code and found some useful comments here: https://code.google.com/searchframe#OAMlx_jo-ck/src/net/udp/udp_socket.h&q=file:(%5E%7C/)net/udp/udp_socket%5C.h$&exact_package=chromium

// Client form:
// In this case, we're connecting to a specific server, so the client will
// usually use:
//       Connect(address)    // Connect to a UDP server
//       Read/Write          // Reads/Writes all go to a single destination
//
// Server form:
// In this case, we want to read/write to many clients which are connecting
// to this server.  First the server 'binds' to an addres, then we read from
// clients and write responses to them.
// Example:
//       Bind(address/port)  // Binds to port for reading from clients
//       RecvFrom/SendTo     // Each read can come from a different client
//                           // Writes need to be directed to a specific
//                           // address.

For the server UDP socket, call chrome.socket.bind and chrome.socket.recvFrom/chrome.socket.sendTo to interact with clients. For the client UDP socket, call chrome.socket.connect and chrome.socket.read/chrome.socket.write to interact with the server.

Here’s an example:

var serverSocket;
var clientSocket;

// From https://developer.chrome.com/trunk/apps/app_hardware.html
var str2ab=function(str) {
  var buf=new ArrayBuffer(str.length);
  var bufView=new Uint8Array(buf);
  for (var i=0; i= 0)
            {
                chrome.socket.sendTo(serverSocket, 
                    str2ab('Received message from client ' + recvFromInfo.address + 
                    ':' + recvFromInfo.port.toString() + ': ' + 
                    ab2str(recvFromInfo.data)), 
                    recvFromInfo.address, recvFromInfo.port, function(){});
                read();
            }
            else
                console.error('Server read error!');
        });
    }

    read();
});

// A client
chrome.socket.create('udp', null, function(createInfo){
    clientSocket = createInfo.socketId;

    chrome.socket.connect(clientSocket, '127.0.0.1', 1345, function(result){
        console.log('chrome.socket.connect: result = ' + result.toString());
    });

    chrome.socket.write(clientSocket, str2ab('Hello server!'), function(writeInfo){
        console.log('writeInfo: ' + writeInfo.bytesWritten + 
            'byte(s) written.');
    });

    chrome.socket.read(clientSocket, 1024, function(readInfo){
        console.log('Client: received response: ' + ab2str(readInfo.data), readInfo);
    });
});