将node.js中的字节数组发送到服务器

时间:2014-01-07 19:11:20

标签: javascript string node.js sockets byte

我无法弄清楚在Debian上连接到服务器时发送字节数组。有没有通过client.write()发送字符串的替代方法?我尝试client.write(new Buffer("something")),但这会导致Invalid data错误。这是我的代码:

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');
});

我也在Windows上使用相同的代码运行客户端,但createServer部分除外。

输出:

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

视窗:

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

非常感谢帮助。

2 个答案:

答案 0 :(得分:4)

有一个BufferStream类here的示例,可用于将缓冲区转换为可读流。

本文有一个通过响应对象管道缓冲流的示例,但是,当client.connect返回实现双工流的net.Socket时,您应该同样能够将缓冲流传输到你的client套接字。

例如:

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
}

一般来说,如果你可以在Node中传输一些内容,你可能应该这样做。它是一个更优雅的模型,可以让你做一些很酷的事情。

在实践中有许多流的例子很有意思,在这里:http://nodestreams.com/

答案 1 :(得分:4)

缓冲区对我来说不起作用,因为我有一个名为node.js的自定义类。这有效:

testBuff = new Buffer([4, 0, -1, 1, 32, 0]);
client.write(testBuff);
相关问题