简体   繁体   English

recv套接字api的内部工作

[英]internal working of the recv socket api

I am working on TCP client server application using c++.third party lib are now allowed in this project. 我正在使用c ++处理TCP客户端服务器应用程序。此项目现在允许第三方lib。

Here exchange between client server takes using well define protocol format.once the client receives the packet it will send it for parsing.I have protocol manager which will take care of the parsing activity. 客户端服务器之间的交换使用了定义良好的协议格式。一旦客户端接收到数据包,它将发送它进行解析。我有协议管理器负责解析活动。

I have following doubt 我有以下疑问
When the data arrives at client from the network, the OS buffers it until application calls recv() function. 当数据从网络到达客户端时,操作系统会对其进行缓冲,直到应用程序调用recv()函数为止。

So two message msg1 and msg2 arrives at the buffer a call to recv will return msg1+msg2. 因此,两个消息msg1和msg2到达缓冲区,对recv的调用将返回msg1 + msg2。 Now this may result in failure of the parsing activity. 现在,这可能会导致解析活动失败。

My queries 我的查询
1. whether above mentioned assumption is correct or not ? 1.上述假设是否正确?
2. If above mentioned assuption is correct then how can resolve this issue. 2.如果上述假设正确,那么如何解决此问题。

TCP emulates a stream, so in TCP there is no notion of messages. TCP模拟流,因此在TCP中没有消息的概念。 If you want messages, your application has to have some protocol to seperate them. 如果您想要消息,则您的应用程序必须具有一些协议来分隔它们。

UDP does have a messages, so there it is possible to retrieve separate messages. UDP确实有一条消息,因此可以检索单独的消息。

You can use a LV protocol (Length-Value) for your message. 您可以为消息使用LV协议(长度值)。 Encode the message length in the first (1-4) bytes, then put the message. 将消息长度编码为前(1-4)个字节,然后放入消息。 Something like this for example : 14"Hello world\\0" 例如:14“ Hello world \\ 0”

In your server, when a client is sending something you'll have to recv() the first (1-4) bytes then recv() the length of the message. 在您的服务器中,当客户端发送邮件时,您必须先接收Recv()前(1-4)个字节,然后再接收消息的长度Recv()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM