i made i client tcp socket with c# and its work without any problem with c# server too
first before i send data to the client i sent the header contain the data length adn some other
now i tried same client code with nodeJs server TCP i got some issuse with Buffer size in nodeJs
first this is my header
function writeInt(stream, int){
var bytes = new Array(5)
bytes[0] = 2
bytes[1] = int >> 24
bytes[2] = int >> 16
bytes[3] = int >> 8
bytes[4] = int
bytes[5] = 0
stream.write( Buffer.from(bytes))
}
//1,2,3,4 the size of the buffer
if i want to send message to client i use
var buf = Buffer.from("HELLO THIS IS A MESSAGE FROM SERVER");
writeInt(sock, buf.length)
sock.write(buf);
that's work because i use string but when i tried to send integer i got issuse
like
var buf = Buffer.from([1,2222,999,666]);
writeInt(sock, buf.length)
sock.write(buf);
first thing the length of the buffer its wrong when i use buf.length
it's return 4
!!
i tried other method found it in NodeJs website like
let buf = Buffer.alloc(3); // Init buffer without writing all data to zeros
buf.writeUInt16BE(220);
and
let buf = Buffer.allocUnsafe(2);
buf.writeUInt16BE(1234);
all those return wrong buffer length
or buffer length not like i sent with function writeInt
that's happen only with Integer
when i sent string its work without any issuse
var buf = Buffer.from([1,2222,999,666]);
buf.length
return4
why ? if i use stringvar buf = Buffer.from("hello");
this will return5
but with first example its return wrong length – Danial Sep 24 '20 at 0:16