最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Send byte array in node.js to server - Stack Overflow

programmeradmin2浏览0评论

I can't figure out to send byte array while being connected to server on Debian. Is there any alternative to sending string via client.write()? I tried client.write(new Buffer("something")) but this gives Invalid data error. Here is my code:

var net     = require('net');

function ModernBuffer(buffer) { // custom buffer class
  this.buffer = new ArrayBuffer(buffer.length);
  this.byteLength = this.buffer.byteLength;
  console.log('ModernBuffer.ByteLength: ' + this.byteLength);

  var Uint16View = new Uint16Array(this.buffer, 0, 1);

  for (var i=0; i<Uint16View.length; i++) {
    Uint16View[i] = buffer[i];
    console.log("Entry " + i + ": " + Uint16View[i]);
  }
}

var client = net.connect({ host: 'someIp.pl', port: 7171 }, function () { // this IP is not important until I figure out how to send correct bytes
    console.log('Server: connected');

  var bytes = Array(6); // array of bytes to be send
  bytes[0] = 0x4;
  bytes[1] = 0x0;
  bytes[2] = 0xFF;
  bytes[3] = 0x01;
  bytes[4] = 0x20;
  bytes[5] = 0x0;

function ab2str(buf) { // First ArrayBuffer to string function...(gives me 4 bytes instead of 6 at the end
       return String.fromCharCode.apply(null, new Int8Array(buf)); 
     }

     function strFromUtf8Ab(ab) { // that gives me 0 bytes!
    return decodeURIComponent(escape(String.fromCharCode.apply(null, ab)));
}


  sendBuffer = new ArrayBuffer(6);
  intView8 = new Int8Array(sendBuffer);
  var str = "";
  for(var i = 0; i < intView8.length; i++) {
    intView8[i] = bytes[i];
    console.log("Intview length: " + intView8.length + " | Entry [" + i + "]: " + intView8[i]);
  }

  //var sendMsg = ab2str(sendBuffer); 4 bytes instead of 6...
  //var sendMsg = strFromUtf8Ab(sendBuffer); 0 bytes instead of 6...
  var binaryString = '';
  var bytes = Array(6);
  bytes[0] = 0x4;
  bytes[1] = 0x0;
  bytes[2] = 0xFF;
  bytes[3] = 0x01;
  bytes[4] = 0x20;
  bytes[5] = 0x0;
  var length = bytes.length;
// another try to convert bytes to string - gives me 7 instead of 6 and they are bad
for (var i = 0; i < length; i++) {
  binaryString += String.fromCharCode(bytes[i]);
}
  sendMsg = binaryString;

// test final string which is sent
  t=[]; 
for(s=unescape(encodeURI(sendMsg)),i=0;i<s.length;++i)
  t.push(s.charCodeAt(i));

console.log(t);

// end

    client.write(sendMsg); // not important until I figure out how to send correct bytes

    var server = net.createServer(function(c) { // test server to send bytes to test-client
  console.log('server connected');
  c.on('end', function() {
    console.log('server disconnected');
  });
  c.write(sendMsg); // send bytes converted to string
});
server.listen(7654, function() {
  console.log('server bound');
});

});


client.on('data', function(data) {

  console.log('----- NEW -----');
    modernBuffer = new ModernBuffer(data);
});

client.on('timeout', function () {
    console.log('Server: timeout');
});

client.on('error', function(error) {
    console.log('Server: error: ' + error);
});

client.on('end', function() {
  console.log('Server: client disconnected');
});

I'm also running a client on Windows with same code except createServer part.

Output:

Debian:

root@ks203255:/home/kuzi/node# node app.js
Server słucha.
Server: connected
Intview length: 6 | Entry [0]: 4
Intview length: 6 | Entry [1]: 0
Intview length: 6 | Entry [2]: -1
Intview length: 6 | Entry [3]: 1
Intview length: 6 | Entry [4]: 32
Intview length: 6 | Entry [5]: 0
[ 4, 0, 195, 191, 1, 32, 0 ] <= final bytes instead of those /\ UP
server bound
Server: client disconnected
server connected

Windows:

C:\Users\Daniel>node app.js
Server słucha.
Server: connected
Intview length: 6 | Entry [0]: 4
Intview length: 6 | Entry [1]: 0
Intview length: 6 | Entry [2]: -1
Intview length: 6 | Entry [3]: 1
Intview length: 6 | Entry [4]: 32
Intview length: 6 | Entry [5]: 0
[ 4, 199, 191, 32 ] <= I'm showing only first 4 bytes but...
----- NEW -----
ModernBuffer.ByteLength: 7 <= you can see it's 7 instead of 6 and they are bad so I'm sure it's wrong
4, 0, 195, 191
Entry 0: 4

Help would be greatly appreciated.

I can't figure out to send byte array while being connected to server on Debian. Is there any alternative to sending string via client.write()? I tried client.write(new Buffer("something")) but this gives Invalid data error. Here is my code:

var net     = require('net');

function ModernBuffer(buffer) { // custom buffer class
  this.buffer = new ArrayBuffer(buffer.length);
  this.byteLength = this.buffer.byteLength;
  console.log('ModernBuffer.ByteLength: ' + this.byteLength);

  var Uint16View = new Uint16Array(this.buffer, 0, 1);

  for (var i=0; i<Uint16View.length; i++) {
    Uint16View[i] = buffer[i];
    console.log("Entry " + i + ": " + Uint16View[i]);
  }
}

var client = net.connect({ host: 'someIp.pl', port: 7171 }, function () { // this IP is not important until I figure out how to send correct bytes
    console.log('Server: connected');

  var bytes = Array(6); // array of bytes to be send
  bytes[0] = 0x4;
  bytes[1] = 0x0;
  bytes[2] = 0xFF;
  bytes[3] = 0x01;
  bytes[4] = 0x20;
  bytes[5] = 0x0;

function ab2str(buf) { // First ArrayBuffer to string function...(gives me 4 bytes instead of 6 at the end
       return String.fromCharCode.apply(null, new Int8Array(buf)); 
     }

     function strFromUtf8Ab(ab) { // that gives me 0 bytes!
    return decodeURIComponent(escape(String.fromCharCode.apply(null, ab)));
}


  sendBuffer = new ArrayBuffer(6);
  intView8 = new Int8Array(sendBuffer);
  var str = "";
  for(var i = 0; i < intView8.length; i++) {
    intView8[i] = bytes[i];
    console.log("Intview length: " + intView8.length + " | Entry [" + i + "]: " + intView8[i]);
  }

  //var sendMsg = ab2str(sendBuffer); 4 bytes instead of 6...
  //var sendMsg = strFromUtf8Ab(sendBuffer); 0 bytes instead of 6...
  var binaryString = '';
  var bytes = Array(6);
  bytes[0] = 0x4;
  bytes[1] = 0x0;
  bytes[2] = 0xFF;
  bytes[3] = 0x01;
  bytes[4] = 0x20;
  bytes[5] = 0x0;
  var length = bytes.length;
// another try to convert bytes to string - gives me 7 instead of 6 and they are bad
for (var i = 0; i < length; i++) {
  binaryString += String.fromCharCode(bytes[i]);
}
  sendMsg = binaryString;

// test final string which is sent
  t=[]; 
for(s=unescape(encodeURI(sendMsg)),i=0;i<s.length;++i)
  t.push(s.charCodeAt(i));

console.log(t);

// end

    client.write(sendMsg); // not important until I figure out how to send correct bytes

    var server = net.createServer(function(c) { // test server to send bytes to test-client
  console.log('server connected');
  c.on('end', function() {
    console.log('server disconnected');
  });
  c.write(sendMsg); // send bytes converted to string
});
server.listen(7654, function() {
  console.log('server bound');
});

});


client.on('data', function(data) {

  console.log('----- NEW -----');
    modernBuffer = new ModernBuffer(data);
});

client.on('timeout', function () {
    console.log('Server: timeout');
});

client.on('error', function(error) {
    console.log('Server: error: ' + error);
});

client.on('end', function() {
  console.log('Server: client disconnected');
});

I'm also running a client on Windows with same code except createServer part.

Output:

Debian:

root@ks203255:/home/kuzi/node# node app.js
Server słucha.
Server: connected
Intview length: 6 | Entry [0]: 4
Intview length: 6 | Entry [1]: 0
Intview length: 6 | Entry [2]: -1
Intview length: 6 | Entry [3]: 1
Intview length: 6 | Entry [4]: 32
Intview length: 6 | Entry [5]: 0
[ 4, 0, 195, 191, 1, 32, 0 ] <= final bytes instead of those /\ UP
server bound
Server: client disconnected
server connected

Windows:

C:\Users\Daniel>node app.js
Server słucha.
Server: connected
Intview length: 6 | Entry [0]: 4
Intview length: 6 | Entry [1]: 0
Intview length: 6 | Entry [2]: -1
Intview length: 6 | Entry [3]: 1
Intview length: 6 | Entry [4]: 32
Intview length: 6 | Entry [5]: 0
[ 4, 199, 191, 32 ] <= I'm showing only first 4 bytes but...
----- NEW -----
ModernBuffer.ByteLength: 7 <= you can see it's 7 instead of 6 and they are bad so I'm sure it's wrong
4, 0, 195, 191
Entry 0: 4

Help would be greatly appreciated.

Share Improve this question edited Aug 24, 2015 at 14:00 Daniel asked Jan 7, 2014 at 19:11 DanielDaniel 18.7k8 gold badges68 silver badges90 bronze badges 3
  • Why are you converting to a string before you send? – Joe Commented Jan 7, 2014 at 19:22
  • Because client.write() takes String. If I try to send something else I get: throw new TypeError('invalid data');. – Daniel Commented Jan 7, 2014 at 19:26
  • 2 If you have the solution, can you write an answer and validate it. – Ortomala Lokni Commented Jul 26, 2014 at 17:07
Add a comment  | 

2 Answers 2

Reset to default 7

There's an example of a BufferStream class here, that can be used for turning buffers into readable streams.

The article has an example of piping a buffer stream through a response object, however, seeing as client.connect returns a net.Socket which implements duplex Stream, you should equally be able to pipe the buffer stream into your client socket.

For example:

var buffer = new Buffer([0x4, 0xFF, 0x01, 0x20, 0x0]),
    stream = new BufferStream(buffer),
    client = net.connect({ host: 'someIp.pl', port: 7171 }, connected);

function connected() {
  stream.pipe(client);
  // do other things
}

Generally speaking, if you can stream something in Node, you probably should. It's a more elegant model and allows you to do some cool things.

Buffer didn't work for me because I had a custom class named exactly as node.js's one. This works:

testBuff = new Buffer([4, 0, -1, 1, 32, 0]);
client.write(testBuff);
发布评论

评论列表(0)

  1. 暂无评论