在TCP流发送消息 [英] Sending Messages in a TCP Stream

查看:112
本文介绍了在TCP流发送消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TCP是基于流的,这意味着你发送的字节没有他们在一个消息一定是,这样的接收器可以得到一半的消息或者一个和消息的三分之二。

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.

所以,像一个游戏,每个消息是固定大小的,如果我收到的消息的一部分,我可以只保存在一个缓冲区,直到我收到另一部分。这是一个有点乏味,但没有任何其他基于消息的可靠的协议?有可能有,但都不是在操作系统中与TCP和UDP实现的,所以我不得不使用一些库,它是罚款,只要它很容易使用。

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.

我总是可以让一个有点可靠的UDP协议。你建议哪一种?

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

推荐答案

您可以实现通过UDP自己的基于ACK的协议。 prePEND与在发送侧的消息/序列号的消息和回声该号码回在接收侧的发送者。在每条消息的发送方启动一个计时器,并取消它,当你得到相应的ACK回来。如果计时器持久性有机污染物,再发送信息。

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是这样,因为这个应用程序的方式太重了。

XMPP is way, way too heavy for this application.

这篇关于在TCP流发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆