4

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.

So in something like a game where each message is fixed-size, if I receive a portion of a message I could just keep it in a buffer until I receive the other part. This is a bit tedious, but is there any other message-based reliable protocol? There probably are, but none are implemented in the OS as with TCP and UDP, so I'll have to use some library, which is fine as long as it's easy to use.

I could always make a somewhat-reliable UDP protocol. Which do you suggest?

slartibartfast
  • 4,348
  • 5
  • 31
  • 46

5 Answers5

2

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.

boettger1
  • 418
  • 1
  • 3
  • 9
  • When this question popped up again, I remembered this is actually what I ended up doing. – slartibartfast Jul 10 '12 at 13:42
  • The notion of "unreliable" around UDP can put people off using it, but mostly it means is that additional reliability measures are not built into the protocol like they are in TCP. In some contexts you may not end up needing them or even reproducing them, e.g. if your application is fault-tolerant, or if the network design and data load reduces faults to tolerable levels. In real-time contexts, these extras might even detract. For your gaming context, UDP used this way is very common. – omatai Nov 25 '14 at 22:50
1

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.

SKi
  • 8,007
  • 2
  • 26
  • 57
  • Linux kernel has support for it. And also BSD kernel implementation exists. I don't know about Windows or userspace libraries. – SKi Mar 29 '12 at 17:07
0

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.

kshahar
  • 10,423
  • 9
  • 49
  • 73
0

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

Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
0

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

dweeves
  • 5,525
  • 22
  • 28
  • Protocol Buffers look interesting. I'll have a look at it. – slartibartfast Mar 29 '12 at 16:55
  • There doesn't seem to be an official C version and the third-party ones aren't yet fully capable. Mentioning this because the question is tagged C, not C++. – Ioan Mar 29 '12 at 17:41
  • 1
    This answer is irrelevant to the question. Protocol buffers does not solve the networking problem, it only provides serialization. – kshahar Jul 10 '12 at 12:52