简体   繁体   中英

nodejs tcp socket write

I am having trouble writing some data to a tcp socket in nodejs.

Here is the adobe flex code i am replacing.

private function SendRequestHello():void
{
    writeUnsignedInt( 0);                                                   /* Request Type. */
    writeUnsignedInt( 16);                                                  /* Length. */
    writeByte( OPCODE_HELLO);                                               /* Opcode. */
    writeUnsignedInt( m_nOwnID / 0x100000000);                              /* GPS Server ID. */
    writeUnsignedInt( m_nOwnID & 0xffffffff);                               /* GPS Server ID part 2. */
    writeUnsignedInt( 3 << 24);                                             /* GPS Server Version (my version). */
    writeUnsignedInt( 3 << 24);                                             /* Oldest supported Server Version. */
    FlushWithCatch();
}

and here is my attempt in node

clients[socket.id].fwd.connect(26000, clients[socket.id].serverip, function() {
    console.info(socket.id + ' connected to gps forwarder: ' + clients[socket.id].serverip + ':26000');
    clients[socket.id].fwdConnected = true;

    clients[socket.id].fwd.write( 0);                                                   /* Request Type. */
    clients[socket.id].fwd.write( 16);                                                  /* Length. */
    clients[socket.id].fwd.write( 1);                                                   /* Opcode. */
    clients[socket.id].fwd.write( clients[socket.id].AppID / 0x100000000);              /* GPS Server ID. */
    clients[socket.id].fwd.write( clients[socket.id].AppID & 0xffffffff);               /* GPS Server ID part 2. */
    clients[socket.id].fwd.write( 3 << 24);                                             /* GPS Server Version (my version). */
    clients[socket.id].fwd.write( 3 << 24);                                             /* Oldest supported Server Version. */
}); 

I get TypeError: invalid data

TypeError: invalid data
at Socket.write (net.js:617:11)
at Socket.<anonymous> (C:\**\index.js:84:30)
at Socket.g (events.js:260:16)
at emitNone (events.js:72:20)
at Socket.emit (events.js:166:7)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1045:10)

Are there any different write options i should be using here?

EDIT: https://github.com/yi/node-bytearray Looks like an interesting plugin, but i do not understand the usage for it. How would i 'writeUnsignedInt' into my net socket?

My solution

var buffer = new Buffer(25);
var writer = new BinaryWriter(buffer);
writer.writeUInt32BE(0);            
writer.writeUInt32BE(16);           
writer.writeUInt8(1);                           
writer.writeUInt32BE(client.AppID / 0x100000000);   
writer.writeUInt32BE(client.AppID & 0xffffffff);    
writer.writeUInt32BE(3 << 24);                      
writer.writeUInt32BE(3 << 24);                      

client.fwd.write(buffer);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM