node_client.js
var net = require('net');
var client = net.connect({port: 8081, host: '192.168.1.105'},
function() {
console.log('connected');
client.write('world!\r\n');
});
client.on('data',
function(data) {
console.log(data.toString());
client.end();
}
);
client.on('end',
function() {
console.log('client disconnected');
}
);
node_server.js (same as in last post)
var os=require('os');
var net=require('net');
var networkInterfaces=os.networkInterfaces();
var port = 8081;
var count = 1;
function callback_server_connection(socket){
var remoteAddress = socket.remoteAddress;
var remotePort = socket.remotePort;
socket.setNoDelay(true);
console.log("connected: ", remoteAddress, " : ", remotePort);
var msg = 'Hello ' + remoteAddress + ' : ' + remotePort + '\r\n'
+ "You are #" + count + '\r\n';
count++;
socket.end(msg);
socket.on('data', function (data) {
console.log(data.toString());
});
socket.on('end', function () {
console.log("ended: ", remoteAddress, " : ", remotePort);
});
}
console.log("http://android-er.blogspot.com/");
console.log("http://helloraspberrypi.blogspot.com/");
console.log("node.js net server is waiting:");
for (var interface in networkInterfaces) {
networkInterfaces[interface].forEach(function(details){
if ((details.family=='IPv4') && !details.internal) {
console.log(interface, details.address);
}
});
}
console.log("port: ", port);
var netServer = net.createServer(callback_server_connection);
netServer.listen(port);
No comments:
Post a Comment