Created
October 30, 2011 01:41
-
-
Save cronopio/1325350 to your computer and use it in GitHub Desktop.
nodejs-demos
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var net = require('net'), | |
dns = require('dns'); | |
function checkComando (data, socket) { | |
var c_raw = data.toString(); | |
comando = c_raw.slice(0, -2).split(' '); | |
if (comando[0] === 'ip') { | |
console.log('Solicitando: ', comando[1]); | |
dns.resolve(comando[1], 'A', function(err, info) { | |
if (info !== undefined) { | |
socket.write('[' + info.join(', ') + ']'); | |
} | |
}); | |
} | |
if (comando[0] === 'salir') { | |
console.log('%s se desconecto.', socket.remoteAddress); | |
socket.write('Adios.\n', function() { | |
socket.end(); | |
}); | |
} | |
} | |
function saludar (socket) { | |
console.log('Nuevo cliente: ', socket.remoteAddress); | |
socket.write('Bienvenido al servidor de cronopio.\n', 'UTF-8', function() { | |
socket.on('data', function (data) { | |
checkComando(data, socket); | |
}); | |
}); | |
} | |
var servidor = net.createServer(saludar); | |
servidor.listen(8000); | |
console.log('Servidor corriendo.'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var puts = require('util').puts; | |
setInterval(function() { | |
puts('Hola'); | |
}, 1000); | |
process.addListener("SIGINT", function() { | |
puts('El usuario quiere cerrar'); | |
process.exit(0); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'); | |
var servidor = http.createServer(function(peticion, respuesta) { | |
respuesta.writeHead(200, {"Content-Type": "text/plain"}); | |
respuesta.write('Bienvenido al servidor de cronopio'); | |
respuesta.end(); | |
}); | |
servidor.listen(4000); | |
console.log('Servidor corriendo.') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var util = require('util'); | |
setTimeout(function() { | |
util.puts('mundo!'); | |
}, 3000); | |
util.puts('Hola'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var net = require('net'); | |
var servidor = net.createServer(function(c) { | |
console.log('Conexion desde', c.remoteAddress); | |
c.write('Bienvenido al servidor de cronopio.\n', 'UTF-8', function() { | |
c.end(); | |
}); | |
}); | |
servidor.listen(8000); | |
console.log('Servidor corriendo.'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment