You are not taking endian or byte size into account.
The integer overloads of TIdIOHandler.Write()
accept a numeric value in host byte order, which may be either little-endian or big-endian depending on the platform, and will convert them for transmission to network byte order by default, which is big endian. The integer TIdIOHandler.Read...()
methods expect numeric values to arrive in network byte order by default, and will convert them to host by order by default. So, if you are using Indy on both sides, you do not have to worry about this detail, since the endian of the reads/writes would be consistent regardless of which platform is on either end.
The TIdIOHandler.Write()
numeric overloads, and the TIdIOHandler.Read...()
numeric methods, all have an optional Convert
parameter that is True by default. If you set this to False instead, values will be sent/read in host byte order instead of network byte order.
What is more important is that you are reading an Int64
, but you are not sending an Int64
. There are multiple overloads for TIdIOHandler.Write()
for the various integer types, and an untyped 19538
constant would never resolve to Int64
with such overloads present. So you need to use a type-cast to tell the compiler to use the Int64
overload:
Client.IOHandler.Write(Int64(19538));
Or use a variable:
var
Value: Int64;
Value := 19538;
Client.IOHandler.Write(Value);
Pull code of server: AContext.Connection.IOHandler.ReadInt64; //receives 5930114809340100608
19538 in hex is 0x4C52
.
5930114809340100608 in hex is 0x524C000000000000
.
Notice a similarity?
19538 in network byte order is 4C 52
as a 16bit integer, and as 00 00 4C 52
as a 32bit integer. That is only 2-4 bytes, but an Int64
is 8 bytes, so there has to be another 4-6 bytes present otherwise ReadInt64
would not return. But lets assume there were extra 00
bytes present (in which case you are not handling your protocol data correctly). In order for ReadInt64
to return 5930114809340100608 from 8 bytes in network byte order, it would had to have received 52 4C 00 00 00 00 00 00
, which does not come close to matching 19538 as a 16bit or 32bit integer in network byte order.