简体   繁体   English

如何在python TCP套接字调用send()和recv()中做一个标记

[英]How to make an endmark in python TCP socket calls send() and recv()

I am relatively new to sockets and very new to python. 我对套接字相对较新,而对python较新。 How would you go about making an endmark for python send() and recv(). 您将如何为python send()和recv()做一个标记。

I have searched all over and there is no easy tutorial. 我到处搜索,没有简单的教程。 I have read the man page for recv(2) a thousand times and it ironically makes less sense to me each time I read it. 我已经阅读了recv(2)的手册页一千次,但讽刺的是,每次阅读它对我来说意义不大。

I would like to use the send() function in a server to let the client calling recv() know when the end of the send() is. 我想在服务器中使用send()函数,以使调用recv()的客户端知道send()的结束时间。

Do you use the flag argument of send? 您是否使用send的flag参数?

Or do you use something like "|".join(str1, str2) and use an if statement in the client to recognize the | 还是使用“ |” .join(str1,str2)之类的东西,并在客户端使用if语句来识别| and parse the statement? 并解析该语句?

TCP is not message oriented protocol. TCP不是面向消息的协议。 It does not maintain message boundaries nor it does not help in other ways to achieve it. 它不维护消息边界,也无法以其他方式帮助实现它。 It is up-to the application to mark boundaries. 标记边界由应用程序决定。 Client and server can agree upon a method in exchange of data. 客户端和服务器可以商定一种数据交换方法。 Common method is to put in the message length in the data you send. 常用的方法是在发送的数据中放入消息长度。

[2 byte message length][Actual Data of Interest]

The end which receives packets will always look for two byte length indicator. 接收数据包的一端将始终寻找两个字节的长度指示符。 recv as much data indicated by length bytes, process them and again go to recv length bytes and so on. recv由长度字节指示的尽可能多的数据,对其进行处理,然后再次转到recv长度字节,依此类推。

Another method is that the application can mark the start and end of the message with markers. 另一种方法是应用程序可以用标记标记消息的开始和结束。 It also needs to handle cases where the markers can also be part of actual data. 它还需要处理标记也可以是实际数据的一部分的情况。

[Start Indicator][Actual Data of Interest][ End Indicator]

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

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