3

I'm trying to get up to speed on Node.js and one thing I have been trying to accomplish is to mimic a simple file transfer agent with Node acting as the listening server. This would require node listening to tcp socket requests and then accepting the binary data stream and storing it in the system.

What I have so far is this, where the server still doesn't store anything just outputs the data it receives, or it should.

Server:

var net = require('net');
var fs = require('fs');
var buffer = require('buffer');

var server = net.createServer(function() {
    console.log('server connected');
});

var HOST = '127.0.0.1';
var PORT = '9001'
var FILEPATH = '/home/steve/Downloads/';


server.listen(PORT, HOST, function() {
    //listening
    console.log('server bound to ' + PORT + '\n');

    server.on('connection', function(){
        console.log('connection made...\n')
    })

    server.on('data', function(data) {
        console.log('data received');
        console.log('data is: \n' + data);
    });


});

And just to keep things simple, I wrote a hack of a client in Node as well.

Client:

var net = require('net');
var fs = require('fs');


var PORT = 9001;
var HOST = '127.0.0.1';
var FILEPATH = '/home/steve/Work/Node.js/FileTransfer/random.fil';

var client = new net.Socket()

//connect to the server
client.connect(PORT,HOST,function() {
    'Client Connected to server'

    //send a file to the server
    var fileStream = fs.createReadStream(FILEPATH);
    fileStream.on('error', function(err){
        console.log(err);
    })

    fileStream.on('open',function() {
        fileStream.pipe(client);
    });

});

//handle closed
client.on('close', function() {
    console.log('server closed connection')
});

client.on('error', function(err) {
    console.log(err);
});

When I run them, I don't get any errors, but the server simply creates the server, accepts the connection then closes it. I can't seem to figure it out beyond that. Also, would this be a decent solution to transferring large files, in ~1GB range or is Node simply not geared for something like this?

TIA

SteveMustafa
  • 581
  • 2
  • 7
  • 18

2 Answers2

5

I think you're misusing the server. When you call net.createServer the callback that gets passed there is called every time a new connection is made. The parameter that gets passed in is the connection itself to which you listen for data events. I think this is more what you want:

var net = require('net');
var fs = require('fs');
var buffer = require('buffer');

var server = net.createServer(function(conn) {
    console.log('server connected');

    conn.on('data', function(data) {
        console.log('data received');
        console.log('data is: \n' + data);
    });
});

var HOST = '127.0.0.1';
var PORT = '9001'
var FILEPATH = '/home/steve/Downloads/';


server.listen(PORT, HOST, function() {
    //listening
    console.log('server bound to ' + PORT + '\n');

    server.on('connection', function(){
        console.log('connection made...\n')
    })
});

Or, you could do it in the servers connection event. That also gets passed a connection object:

var net = require('net');
var fs = require('fs');
var buffer = require('buffer');

var server = net.createServer(function(conn) {
    console.log('server connected');

});

var HOST = '127.0.0.1';
var PORT = '9001'
var FILEPATH = '/home/steve/Downloads/';

server.listen(PORT, HOST, function() {
    //listening
    console.log('server bound to ' + PORT + '\n');

    server.on('connection', function(conn) {
        console.log('connection made...\n')
        conn.on('data', function(data) {
            console.log('data received');
            console.log('data is: \n' + data);
        });
    })
});

The point is, you listen for data on a connection, not on the server itself.

BFree
  • 97,931
  • 20
  • 150
  • 197
  • Bless, you! That worked from the first go. So, if I were to continue this example, how does the pipe of binary data to a specific file path work? Also, in your experience, is Node.js suitable for such a server or should I stick to Python atop twisted? – SteveMustafa Feb 14 '13 at 00:35
  • OK, so I've created a writeable file stream from fs and I've piped the connection to the fs as so: var fileStream = fs.createWriteStream(FILEPATH + 'output.bin'); conn.pipe(fileStream); Now, I've noticed two things. 1) The MD5sum of both files are NOT the same which I find quite interesting. 2. I'm getting a memory leak warning about 11 emitters attached. 3. I need to learn how to respond rather than comment :s – SteveMustafa Feb 14 '13 at 01:03
0

I don't know why the checksum are different, I tried but it seems to be equals for me. For the second question check this http://nodejs.org/api/events.html#events_emitter_setmaxlisteners_n. API said: "EventEmitters will print a warning if more than 10 listeners are added for a particular event" and "Set to zero for unlimited".

Matteo Baggio
  • 81
  • 1
  • 3
  • Thanks Matteo, I need to re-check things at my end. Thanks for the second answer, but that begs the question, where am I setting or using more than 10 event listeners in the code above? – SteveMustafa Mar 27 '13 at 15:24