This should be easy to figure out but I am getting so frustrated, and I can't seem to find documentation for this rather simple case.
I want to send bytes (not strings) over a TCP connection and process a response. Here's what I've got, but it throws a type exception on the use of a Buffer type. When I use a string type instead, it sends the bytes 0xc3 0xbe 0x74 0x01 instead of 0xfe 0x74 0x01 (from tcpdump). God knows why.
If I should be using the pipe interface instead, then great, but I can't seem to find how to do so for TCP streams and not files.
const net = require ('net')
const pumpIP = '192.168.1.208'
const pumpPort = 2101
const pumpStr = '\xfe\x74\x01'
const pumpBuffer = Buffer.from(0xfe, 0x74, 0x01)
var pump = new net.Socket()
pump.connect(pumpPort, pumpIP, function() {
pump.write(pumpBuffer) // <-- this throws a type error
// pump.write(pumpStr) // <-- this sends 0xc3 0xbe 0x74 0x01 instead
})
pump.on('data', function(data) {
// code to handle data
pump.destroy()
})
For your Buffer.from()
, you need to use an array. Try this:
const pumpBuffer = Buffer.from([0xFE, 0x74, 0x01]);
https://nodejs.org/api/buffer.html#buffer_class_method_buffer_from_array