Tuesday, April 19, 2011

Protocols #4, Daytime

Time for another simple protocol: Daytime

"A useful debugging and measurement tool is a daytime service. A daytime service simply sends a the current date and time as a character string without regard to the input." - RFC 867

This time we will use a more complex Javascript function, the Date().toISOString(), which will return the date in an ISO compliant manner.

As usual the actual port for this protocol is 13, but I don't want to run it as root, yet.
//RFC 867 Daytime in Node.js
var net = require('net');
var dgram = require('dgram');

function gettime() {
var d = new Date();
return d.toISOString() + "\n";
}

//TCP
var tcpserver = net.createServer(function (socket) {
socket.end(gettime());
})

//UDP
var udpserver = dgram.createSocket("udp4", function (msg, rinfo) {
var ret = new Buffer(gettime());
udpserver.send(ret, 0, ret.length, rinfo.port, rinfo.address);
});

//Bind and serve
tcpserver.listen(8013, "127.0.0.1");
udpserver.bind(8013, "127.0.0.1");

No comments:

Post a Comment