The WholePacket NetSimple Methodis great for NetTalk to NetTalk objects since it sends the length as a 4 byte header and can assemble the packets for you saving some code. (See the manual boundary example).
There is a standard way that many non NetTalk TCP applications use. They send the length as a 4 bytes header but it is no Clarion LONG you can distinguish.
The Bytes are sent as a Big Endian ULONG Integer. This is great until you find out Clarion is not Big Endian So we pretend it's a STRING instead and it's all better. You just need a little funny math.
Should Capesoft add an option to WholePacket to use Big Endian longs they could make WholePacket work with other players as well as Clarion and it wouldn't break anyones code since we compute length as a Clarion LONG, but NetTalk could use Big Endian as an option and we wouldn't care since it is doing all the work.
The following functions will convert properly (we tested it today with a an Online service)
ToBigEndian PROCEDURE (LONG RawVal),STRING
ByteArray BYTE,DIM(4)
OutputVal STRING(4),OVER(ByteArray)
CODE
ByteArray[1] = INT(RawVal/256^3)
ByteArray[2] = INT((RawVal % 256^3) / 256^2)
ByteArray[3] = INT((RawVal % 256^2) / 256)
ByteArray[4] = INT(RawVal % 256)
RETURN OutputVal
And to go the other way:
FromBEndian PROCEDURE (STRING RawVal), LONG
ByteArray BYTE,DIM(4)
InputVal STRING(4),OVER(ByteArray)
Result LONG
CODE
InputVal = RawVal
Result = (INT(ByteArray[1]) * 256^3 + |
INT(ByteArray[2]) * 256^2 + |
INT(ByteArray[3]) * 256 + |
INT(ByteArray[4])) ! The packet includes the 4 byte header
RETURN Result
Special thanks to Marc Walgren of MItten Software for being the tester that would have asked me to fix it if it didn't work..