简体   繁体   English

从TCP套接字读取

[英]Reading From a TCP Socket

I have clients sending a request of a very simple form: just the string GET/PUT/INSERT/DELETE KEY <VALUE> where value is optional depending on the selected keyword. 我有客户发送非常简单形式的请求:只是字符串GET/PUT/INSERT/DELETE KEY <VALUE> ,其中值是可选的,具体取决于所选的关键字。

Keys and values can be arbitrarily sized. 键和值的大小可以任意设置。

I am implementing a server in C to service the requests however I think I am running into the issue that the read() system call reads some, but not all data. 我正在C中实现一个服务器来处理请求,但是我认为我遇到了read()系统调用读取一些但不是全部数据的问题。 That is, what the server read is only a subset of what the client sent. 也就是说,服务器读取的内容只是客户端发送的内容的一部分。

How can I know when the entire message has been read? 我怎么知道何时阅读了整个消息?

Example Requests: INSERT KEYkeyKEYkeyKEY VALUEvalueVALUEvalueVALUE 示例请求: INSERT KEYkeyKEYkeyKEY VALUEvalueVALUEvalueVALUE

But the Server Might Read: INSERT KEYkeyKEYkeyKEY VALUEvalueVALUE 但是服务器可能读取: INSERT KEYkeyKEYkeyKEY VALUEvalueVALUE

How can I know the entire message has not yet been received and read? 我怎么知道尚未收到并阅读整个消息?

TCP does not provide any way. TCP不提供任何方式。 TCP is a stream oriented protocol, all it does is send a stream of bytes. TCP是一种面向流的协议,它所做的只是发送字节流。 Its up to you to create wire protocol on top of that stream so that you can detect message boundaries. 由您决定在该流的顶部创建有线协议,以便您可以检测消息边界。

Classic ways 经典方式

  • send a fixed size prefix with the size of the expected message 发送带有预期消息大小的固定大小前缀
  • use a self describing encoding (ber for example) 使用自我描述编码(例如ber)
  • send a delimiter - say 2 blank lines in a text protocol (common for older RFC protocols) 发送定界符-在文本协议中说2个空行(较旧的RFC协议常见)

How can I know the entire message has not yet been received and read? 我怎么知道尚未收到并阅读整个消息?

You need some conventions to know where messages are ending. 您需要一些约定来知道消息在哪里结束。 You parse your input. 您解析您的输入。

just a small hint: you're experiencing a short-read. 只是一个小提示:您正在阅读短篇小说。 As mentioned before typically you would add a fixed-length header that contains the length of the message or parse the message's format (imagine receiving a JSON-formatted String). 如前所述,通常您将添加一个固定长度的标头,其中包含消息的长度或解析消息的格式(假设接收到JSON格式的String)。 You could also switch to a message based protocol such as UDP or SCTP. 您还可以切换到基于消息的协议,例如UDP或SCTP。

Also keep in mind that long-reads could also theoretically happen, ie, you could read two commands with a single network read (or one command and half of another command). 另外请记住,理论上也可能会发生长时间读取,即,您可以通过一次网络读取来读取两个命令(或者一个命令,另一个命令的一半)。

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

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