douyu9159 2014-02-13 11:45
浏览 54

从套接字读取消息时如何获得字节序?

I am using go and protocol buffers. go program sends encoded protocol buffer messages to clients connected to the socket. Now since protocol-buffers is not delimited, clients don't know how much data to read from the socket.

I am planning to prefix the message with the message length, a 32bit integer. So clients can read 4 bytes, get the length and read the full message.

I can put an integer value into the bytes array using binary package. Something like,

binary.Write(buf, binary.LittleEndian, value)

Now the question is, write needs a byte order and how will the receiving end know what is the byte order? Is there a way to deal with this without specifying explicit byte order?

  • 写回答

2条回答 默认 最新

  • dtla92562 2014-02-13 12:09
    关注

    The convention is that network byte order is big endian, but your client and server must agree in any case. Specify the byte order explicitly.

    Edit: Reading the Protobuf docs, it might be better to use proto.EncodeVarint and proto.DecodeVarint to write the length before the message, for the sake of consistency.

    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)