Sending Messages in a TCP Stream

前端 未结 5 1317
予麋鹿
予麋鹿 2021-01-19 07:36

TCP is stream-based, which means you send bytes without them necessarily being in a \"message\", so the receiver may get half a message or one and two thirds of messages.

相关标签:
5条回答
  • 2021-01-19 08:15

    You can take a look at XMPP. It's a TCP/IP based protocol based on XML-messages.

    0 讨论(0)
  • 2021-01-19 08:15

    Protocol Buffers, is less known that XMPP but may be of some interest in your case.

    http://code.google.com/intl/en-US/apis/protocolbuffers/docs/overview.html

    0 讨论(0)
  • 2021-01-19 08:17

    You could implement your own ACK-based protocol over UDP. Prepend the message with a message/sequence number on the sending side and echo that number back to the sender on the receiving side. Start a timer on the sending side for each message and cancel it when you get the corresponding ACK back. If the timer pops, re-send the message.

    XMPP is way, way too heavy for this application.

    0 讨论(0)
  • 2021-01-19 08:17

    If you are looking transport layer protocol, then check SCTP. SCTP is message-oriented like UDP and ensures reliable, in-sequence transport of messages with congestion control like TCP.

    SCTP is not yet widely used. So I suggest to use TCP with some kind of message framing.

    0 讨论(0)
  • 2021-01-19 08:23

    You could use ØMQ (ZeroMQ) as your messaging infrastructure. ZeroMQ provides reliable message passing on top of TCP and other transport mechanisms. It has a C API and a comprehensive guide.

    Note that you would have to use an external library for all peers, but you said that is OK with you.

    0 讨论(0)
提交回复
热议问题