Closed bag7dad closed 3 years ago
Hello, the KingClient expects that the first byte of the message is her type, that is, the first byte of each packet must be her identifier. You can see it better in the KingClient project in the KingClient.cs file in the OnMessageReceived method on line 205. For your code to work it would be enough to send an identifier byte before the string. Can you test and warn here on the thread?
same i tried
var datax1 = {"OperationCode":220,"ReturnCode":0};
sock.write(Buffer.from(JSON.stringify("message")));
sock.write(Buffer.from(JSON.stringify(datax1)));
i sent first value as identifier like example above but same issuse >>> I am using NodeJs as server and your code as client
can you made simple example with nodeJs as server
Can you try something in that style?
var messageType = 0x0A;
sock.write(messageType);
//The first byte of packet
sock.write(Buffer.from(JSON.stringify("message")));
NodeJs error
The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received type number (10)
I managed to create an example, and I found the cause of the problem, the way the node's socket.write writes strings is different from the way that c #, in C # before it puts the string bytes in the buffer, it adds the length of the string first, for example the string "hi", it will contain two bytes one with the character of "h" and the other with the character of "i", in C # something like this: {0x00, 0x00, 0x00, 0x02, 0x104 , 0x105}, the first 4 bytes are the length of the string, as it is in whole it occupies 4 bytes, byte 104 is the "h", and byte 105 is the "i", this is the form of the encoding library .NET handle this in C # to avoid any kind of problem when reading and writing the string, since the node does not do any error handling so write as follows {0x104, 0x105} only "h" and " i, if you want to use the node you would have to write the first 4 bytes required manually, here is a print and the code snippet:
var net = require('net');
var type = new Buffer([0x0A])
var message = Buffer.from('{ "name": "testing", "speed": 10, "life": 50 }');
var messageLength = new Buffer([0x00, 0x00, 0x00, message.length]);
var bufferToSend = type + messageLength + message;
console.log(messageLength);
var server = net.createServer(function(socket) {
socket.write(bufferToSend);
socket.pipe(socket);
});
server.listen(7171, '127.0.0.1');
Remembering that you still need to inform the message type as the first byte before anything.
thank you
var datax = {"aaaa":220,"bbbb":0};
const dataBuffer = Buffer.from(JSON.stringify(datax))
sock.write(dataBuffer)
with Node Js server