套接字编程:的recv() [英] Socket-programming: recv()

查看:144
本文介绍了套接字编程:的recv()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在其中的各种实体通过套接字相互通信的应用程序,我使用C语言编程。当一个实体发送一个长消息给另一个实体,所述的recv()函数可以读取在部件此消息。因此,我通过追加所有接收到的部分重新构造了接收方的消息。

I have an application in which various entities communicate to each other via sockets and I'm using the C programming language. When an entity sends a long message to another entity, the recv() function may read this message in parts. Therefore, I have to re-construct the message on the recipient side by appending all the received parts.

我的问题是有关的recv一般的socket编程问题()。怎样的recv()什么时候已经有消息完全看?我应该终止的消息中包含\\ n特殊字符?或者我应该发送邮件的大小作为一个标题?什么是常见的做法?

My question is a general socket programming question related to recv(). How does recv() know when a message has been fully read? Should I terminate a message with a special character like "\n"? or should I send the size of the message as a header? What is the common practice?

推荐答案

当你已经注意到了,与流套接字没有内置的消息边界的概念。你需要建立测定结束的消息到你的应用级协议的一些方法。

As you've noticed, with stream sockets there is no built-in notion of message boundaries. You need to build some way of determining the end-of-message into your application-level protocol.

这两个你建议的选项是共同的:无论是长度preFIX(首先是消息的长度每个消息)或消息结束的分隔符(这可能只是在文本换行基于协议,例如)。第三,较少使用的,选择是要求一个固定大小为每条消息。这些选项的组合也是可能的 - 例如,固定大小的报头,其包括长度值

Both of the options you've suggested are common: either a length prefix (starting each message with the length of the message) or an end-of-message delimiter (which might just be a newline in a text-based protocol, for example). A third, lesser-used, option is to mandate a fixed size for each message. Combinations of these options are also possible - for example, a fixed-size header that includes a length value.

这篇关于套接字编程:的recv()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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