{"id":61,"date":"2011-04-13T17:52:36","date_gmt":"2011-04-13T17:52:36","guid":{"rendered":"http:\/\/beta.robertprice.co.uk\/robblog\/2011\/04\/writing_a_daytime_server_in_node_js-shtml\/"},"modified":"2011-04-13T17:52:36","modified_gmt":"2011-04-13T17:52:36","slug":"writing_a_daytime_server_in_node_js-shtml","status":"publish","type":"post","link":"https:\/\/www.robertprice.co.uk\/robblog\/writing_a_daytime_server_in_node_js-shtml\/","title":{"rendered":"Writing A Daytime Server In Node.js"},"content":{"rendered":"<p>\nI&#8217;ve previously written about using node.js as a simple UDP listener, now I thought I&#8217;d expand on this and give an example of a node.js application that works as both a UDP and TCP server.\n<\/p>\n<p>\nA simple service that offers both UDP and TCP connections would be the Daytime Protocol, as defined in <a href=\"http:\/\/tools.ietf.org\/html\/rfc867\">RFC 867<\/a>. It listens to port 13 for both TCP and UDP connections, and returns the current date and time. Your local computer may well have this function enabled, to test the TCP version, you can simply telnet to port 13 and see what is returned.\n<\/p>\n<pre class=\"lang:sh decode:true \" >telnet localhost 13\n<\/pre>\n<p>\nIf you find you don&#8217;t have a local Daytime service running, try calling the one at <var>time.ien.it<\/var>, you should see something like this&#8230;\n<\/p>\n<pre class=\"lang:sh decode:true \" >$ telnet time.ien.it 13\nTrying 193.204.114.105...\nConnected to ntp.ien.it.\nEscape character is '^]'.\n12 APR 2011 18:51:11 CEST\nConnection closed by foreign host.\n<\/pre>\n<p>\nAs you can see it simply returns the date and time before closing the connection.\n<\/p>\n<p>\nWe could write something like this very easily in node.js. In this case we&#8217;ll return the date in JavaScript&#8217;s UTC string format instead to keep it simple.\n<\/p>\n<pre class=\"lang:js decode:true \" >var net = require('net');\nvar port = 1300;\nvar now = function() {\n  var date = new Date();\n  return new Buffer(date.toUTCString() + \"rn\");\n};\nvar tcpserver = net.createServer(function(c) {\n  c.write(now());\n  c.end();\n});\ntcpserver.listen(port);\n<\/pre>\n<p>\nWe can test this by telneting to localhost on port 13\n<\/p>\n<pre class=\"lang:sh decode:true \" >\n$ telnet localhost 1300\nTrying 127.0.0.1...\nConnected to localhost.\nEscape character is '^]'.\nTue, 12 Apr 2011 17:17:55 GMT\nConnection closed by foreign host.\n<\/pre>\n<p>\nLet&#8217;s take a quick detour into what the code is doing if you&#8217;ve not used node.js for any network programming before.\n<\/p>\n<p>\nWe <code>require('net')<\/code> so we can use node.js&#8217;s network socket support.\n<\/p>\n<p>\nThe <code>now()<\/code> function simply returns a <code>Buffer<\/code> with the current UTC date and time string in. A <code>Buffer<\/code> in node.js refers to the raw memory used behind the data, in this case our string with the date and time in.\n<\/p>\n<p>\nFinally, we create the server, and add a callback function that is called when something connects to the socket. In this case we&#8217;re just writing out the contents of the <code>now()<\/code> function and ending the connection. Last but not least, we tell the socket which port to listen on.\n<\/p>\n<p>\nWe can see that this code works, but the Daytime protocol also offers a UDP service on the same port that can return the date and time string to a client.\n<\/p>\n<p>\nLet&#8217;s add in some code to support this. We&#8217;ll assume our <code>now()<\/code> function and <var>port<\/var> variable are available already.\n<\/p>\n<pre class=\"lang:js decode:true \" >var dgram = require('dgram');\nvar udpserver = dgram.createSocket(\"udp4\", function(msg, rinfo) {\n  var daytime = now();\n  udpserver.send(daytime, 0, daytime.length, rinfo.port, rinfo.address);\n});\nudpserver.bind(port);\n<\/pre>\n<p>\nSo what is going on here? Well like the TCP code, we have to <code>require('dgram');<\/code> to have access to node.js&#8217;s UDP support.\n<\/p>\n<p>\nNext we create the socket and create a callback function that returns the date and time string back to the IP address and port of the calling computer. Finally we bind this to the port so we start listening for messages.\n<\/p>\n<p>\nIt&#8217;s harder to test a UDP server as we can&#8217;t telnet in, we will have to write a simple UDP client to send a blank message to the server and print out any replies to the console.\n<\/p>\n<pre class=\"lang:js decode:true \" >var dgram = require('dgram');\nvar message = new Buffer(\" \");\nvar server_ip = '127.0.0.1';\nvar server_port = 43278;\nvar client = dgram.createSocket(\"udp4\");\nclient.on('message', function (msg) {\n  console.log(msg.toString());\n  client.close();\n});\nclient.send(message, 0, message.length, server_port, server_ip);\n<\/pre>\n<p>\nWhen you run this client on the same machine as your server you should get back the current date and time.\n<\/p>\n<p>\nHere&#8217;s the final code for our Daytime server.\n<\/p>\n<pre class=\"lang:js decode:true \" >var net = require('net');\nvar dgram = require('dgram');\nvar port = 1300;\nvar now = function() {\n  var date = new Date();\n  return new Buffer(date.toUTCString() + \"rn\");\n}\nvar tcpserver = net.createServer(function(c) {\n  c.write(now());\n  c.end();\n});\ntcpserver.listen(port);\nvar udpserver = dgram.createSocket(\"udp4\", function(msg, rinfo) {\n  var daytime = now();\n  udpserver.send(daytime, 0, daytime.length, rinfo.port, rinfo.address);\n});\nudpserver.bind(port);\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve previously written about using node.js as a simple UDP listener, now I thought I&#8217;d expand on this and give an example of a node.js application that works as both a UDP and TCP server. A simple service that offers both UDP and TCP connections would be the Daytime Protocol, as defined in RFC 867. &hellip; <a href=\"https:\/\/www.robertprice.co.uk\/robblog\/writing_a_daytime_server_in_node_js-shtml\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Writing A Daytime Server In Node.js&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[2],"tags":[31,44,69,75],"class_list":["post-61","post","type-post","status-publish","format-standard","hentry","category-dev","tag-javascript","tag-node-js","tag-udp","tag-web-development"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Writing A Daytime Server In Node.js - Robert Price<\/title>\n<meta name=\"description\" content=\"An example of writing a combined TCP\/IP and UDP Daytime server using node.js\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.robertprice.co.uk\/robblog\/writing_a_daytime_server_in_node_js-shtml\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Writing A Daytime Server In Node.js - Robert Price\" \/>\n<meta property=\"og:description\" content=\"An example of writing a combined TCP\/IP and UDP Daytime server using node.js\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.robertprice.co.uk\/robblog\/writing_a_daytime_server_in_node_js-shtml\/\" \/>\n<meta property=\"og:site_name\" content=\"Robert Price\" \/>\n<meta property=\"article:published_time\" content=\"2011-04-13T17:52:36+00:00\" \/>\n<meta name=\"author\" content=\"rob\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"rob\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/writing_a_daytime_server_in_node_js-shtml\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/writing_a_daytime_server_in_node_js-shtml\\\/\"},\"author\":{\"name\":\"rob\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"headline\":\"Writing A Daytime Server In Node.js\",\"datePublished\":\"2011-04-13T17:52:36+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/writing_a_daytime_server_in_node_js-shtml\\\/\"},\"wordCount\":494,\"keywords\":[\"JavaScript\",\"node.js\",\"UDP\",\"Web Development\"],\"articleSection\":[\"Dev\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/writing_a_daytime_server_in_node_js-shtml\\\/\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/writing_a_daytime_server_in_node_js-shtml\\\/\",\"name\":\"Writing A Daytime Server In Node.js - Robert Price\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\"},\"datePublished\":\"2011-04-13T17:52:36+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"description\":\"An example of writing a combined TCP\\\/IP and UDP Daytime server using node.js\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/writing_a_daytime_server_in_node_js-shtml\\\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/writing_a_daytime_server_in_node_js-shtml\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/writing_a_daytime_server_in_node_js-shtml\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Writing A Daytime Server In Node.js\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\",\"name\":\"Robert Price\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\",\"name\":\"rob\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"caption\":\"rob\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Writing A Daytime Server In Node.js - Robert Price","description":"An example of writing a combined TCP\/IP and UDP Daytime server using node.js","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.robertprice.co.uk\/robblog\/writing_a_daytime_server_in_node_js-shtml\/","og_locale":"en_GB","og_type":"article","og_title":"Writing A Daytime Server In Node.js - Robert Price","og_description":"An example of writing a combined TCP\/IP and UDP Daytime server using node.js","og_url":"https:\/\/www.robertprice.co.uk\/robblog\/writing_a_daytime_server_in_node_js-shtml\/","og_site_name":"Robert Price","article_published_time":"2011-04-13T17:52:36+00:00","author":"rob","twitter_card":"summary_large_image","twitter_misc":{"Written by":"rob","Estimated reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.robertprice.co.uk\/robblog\/writing_a_daytime_server_in_node_js-shtml\/#article","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/writing_a_daytime_server_in_node_js-shtml\/"},"author":{"name":"rob","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"headline":"Writing A Daytime Server In Node.js","datePublished":"2011-04-13T17:52:36+00:00","mainEntityOfPage":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/writing_a_daytime_server_in_node_js-shtml\/"},"wordCount":494,"keywords":["JavaScript","node.js","UDP","Web Development"],"articleSection":["Dev"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"https:\/\/www.robertprice.co.uk\/robblog\/writing_a_daytime_server_in_node_js-shtml\/","url":"https:\/\/www.robertprice.co.uk\/robblog\/writing_a_daytime_server_in_node_js-shtml\/","name":"Writing A Daytime Server In Node.js - Robert Price","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website"},"datePublished":"2011-04-13T17:52:36+00:00","author":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"description":"An example of writing a combined TCP\/IP and UDP Daytime server using node.js","breadcrumb":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/writing_a_daytime_server_in_node_js-shtml\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.robertprice.co.uk\/robblog\/writing_a_daytime_server_in_node_js-shtml\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.robertprice.co.uk\/robblog\/writing_a_daytime_server_in_node_js-shtml\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.robertprice.co.uk\/robblog\/"},{"@type":"ListItem","position":2,"name":"Writing A Daytime Server In Node.js"}]},{"@type":"WebSite","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website","url":"https:\/\/www.robertprice.co.uk\/robblog\/","name":"Robert Price","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.robertprice.co.uk\/robblog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Person","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5","name":"rob","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","caption":"rob"}}]}},"_links":{"self":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/61","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/comments?post=61"}],"version-history":[{"count":0,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/61\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/media?parent=61"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/categories?post=61"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/tags?post=61"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}