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.
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. 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
But the Server Might Read: INSERT KEYkeyKEYkeyKEY VALUEvalueVALUE
How can I know the entire message has not yet been received and read?
TCP does not provide any way. TCP is a stream oriented protocol, all it does is send a stream of bytes. Its up to you to create wire protocol on top of that stream so that you can detect message boundaries.
Classic ways
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). You could also switch to a message based protocol such as UDP or 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).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.