Thursday 30 November 2017

How do I write a buffer to a net TCP stream and send it to a server?

I'm still kind of finding my way around Node and net. Here is the code I have. I do add the port and host for testing.I got most of this code from an example in node docs: http://ift.tt/2AO26hk net = require('net'); const buf = Buffer.allocUnsafe(9); const client = net.createConnection({ [port,] [host] }, () => { buf.write('foo'); buf.writeUInt8(5, 3); buf.write('world'); console.log('connected to server!'); client.write(buf); }); client.on('data', (data) => { console.log('data is ' + data.toString()); client.end(); }); client.on('end', () => { console.log('disconnected from server'); }); Its saying 'connected to server!' but thats all im getting. I just know it wants a message starting with 'foo', the length of the rest of the message, and then 'world'.Anyone see any mistakes or able to offer advice?

Submitted December 01, 2017 at 01:33AM by BikesNotBums

No comments:

Post a Comment