{"id":2192,"date":"2022-08-30T15:22:49","date_gmt":"2022-08-30T15:22:49","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/01\/04\/chrome-packaged-app-udp-sockets-not-working-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:22:49","modified_gmt":"2022-08-30T15:22:49","slug":"chrome-packaged-app-udp-sockets-not-working-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/chrome-packaged-app-udp-sockets-not-working-collection-of-common-programming-errors\/","title":{"rendered":"Chrome packaged app UDP sockets not working-Collection of common programming errors"},"content":{"rendered":"<p>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&#8217;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&amp;q=file:(%5E%7C\/)net\/udp\/udp_socket%5C.h$&amp;exact_package=chromium<\/p>\n<pre><code>\/\/ Client form:\n\/\/ In this case, we're connecting to a specific server, so the client will\n\/\/ usually use:\n\/\/       Connect(address)    \/\/ Connect to a UDP server\n\/\/       Read\/Write          \/\/ Reads\/Writes all go to a single destination\n\/\/\n\/\/ Server form:\n\/\/ In this case, we want to read\/write to many clients which are connecting\n\/\/ to this server.  First the server 'binds' to an addres, then we read from\n\/\/ clients and write responses to them.\n\/\/ Example:\n\/\/       Bind(address\/port)  \/\/ Binds to port for reading from clients\n\/\/       RecvFrom\/SendTo     \/\/ Each read can come from a different client\n\/\/                           \/\/ Writes need to be directed to a specific\n\/\/                           \/\/ address.\n<\/code><\/pre>\n<p>For the server UDP socket, call <code>chrome.socket.bind<\/code> and <code>chrome.socket.recvFrom<\/code>\/<code>chrome.socket.sendTo<\/code> to interact with clients. For the client UDP socket, call <code>chrome.socket.connect<\/code> and <code>chrome.socket.read<\/code>\/<code>chrome.socket.write<\/code> to interact with the server.<\/p>\n<p>Here&#8217;s an example:<\/p>\n<pre><code>var serverSocket;\nvar clientSocket;\n\n\/\/ From https:\/\/developer.chrome.com\/trunk\/apps\/app_hardware.html\nvar str2ab=function(str) {\n  var buf=new ArrayBuffer(str.length);\n  var bufView=new Uint8Array(buf);\n  for (var i=0; i= 0)\n            {\n                chrome.socket.sendTo(serverSocket, \n                    str2ab('Received message from client ' + recvFromInfo.address + \n                    ':' + recvFromInfo.port.toString() + ': ' + \n                    ab2str(recvFromInfo.data)), \n                    recvFromInfo.address, recvFromInfo.port, function(){});\n                read();\n            }\n            else\n                console.error('Server read error!');\n        });\n    }\n\n    read();\n});\n\n\/\/ A client\nchrome.socket.create('udp', null, function(createInfo){\n    clientSocket = createInfo.socketId;\n\n    chrome.socket.connect(clientSocket, '127.0.0.1', 1345, function(result){\n        console.log('chrome.socket.connect: result = ' + result.toString());\n    });\n\n    chrome.socket.write(clientSocket, str2ab('Hello server!'), function(writeInfo){\n        console.log('writeInfo: ' + writeInfo.bytesWritten + \n            'byte(s) written.');\n    });\n\n    chrome.socket.read(clientSocket, 1024, function(readInfo){\n        console.log('Client: received response: ' + ab2str(readInfo.data), readInfo);\n    });\n});\n<\/code><\/pre>\n<p id=\"rop\"><small>Originally posted 2014-01-04 14:17:10. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>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&#8217;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&amp;q=file:(%5E%7C\/)net\/udp\/udp_socket%5C.h$&amp;exact_package=chromium \/\/ Client form: \/\/ In this case, we&#8217;re connecting to a specific server, so the client will \/\/ usually use: \/\/ Connect(address) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2192","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2192","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2192"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2192\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2192"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}