Search code examples
delphiindydelphi-10.3-rio

What is the purpose of GStack.HostToNetwork in Indy?


I saw that Indy, by default, coverts data which must be sent in some "network format", and when is received, is converted back. What is the point of doing this ? Between the Write and the Read from the socket no one uses those values that only "I" know what they means. So, what does it matter the bit order ? I have my own TCP Server-Client protocol and I write drectly to a temporary buffer the values of the commands and other stuff I send. I want to know if it makes sense to complicate my protocol with those conversions ?


Solution

  • I saw that Indy, by default, coverts data which must be sent in some "network format", and when is received, is converted back. What is the point of doing this ?

    Not all platforms use the same endian for ordering the bytes in a multi-byte integer. Some platforms are little-endian, some are big-endian. To facilitate sharing of integers across platforms, "Network Byte Order" is standardized as big-endian. Thus, HostToNetwork() will convert a multi-byte integer from the calling platform's native endian to big-endian, and NetworkToHost() will convert a multi-byte integer from big-endian to the calling platform's native endian.

    Between the Write and the Read from the socket no one uses those values that only "I" know what they means. So, what does it matter the bit order ?

    If you are writing both the sender and the receiver, then of course you can use whatever endian you want. But it is customary to always use only big-endian when transmitting integers over a network connection. Maybe someday, you or someone else will write a different sender/receiver for your system, maybe even on another platform. So if you use a standardized endian for your integers, you won't have to worry about interoperability issues later on.

    I have my own TCP Server-Client protocol and I write drectly to a temporary buffer the values of the commands and other stuff I send.

    That will, of course, only work if the sender and the receiver represent integers in memory using the same endian.

    I want to know if it makes sense to complicate my protocol with those conversions ?

    Doing endian conversions does not complicate anything, but it does safe-guard it.