How to convert a record from host to network byte order (big endian)?

前端 未结 2 1139
既然无缘
既然无缘 2020-12-21 22:49

I have a record:

  Tconnecting = record
  var
    a: int64;
    b: integer;
    c: integer;
  end;

which I need send to server using UDP pr

2条回答
  •  时光说笑
    2020-12-21 23:13

    What you are actually doing is converting from host byte order to network byte order. All standard sockets libraries provide helper functions to do that.

    For example, Winsock offers htons, htonl etc. And in the opposite direction you have ntohs, ntohl etc. If you are using Indy, then the equivalent functions are GStack.HostToNetwork and GStack.NetworkToHost.

    You should serialize each field into a byte stream, with each field being transformed by the appropriate hton* function. Then you can put the byte stream on the wire.

提交回复
热议问题