0

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?

1
  • 2
    If you specify that the client must end each string with a carriage return, or a NUL/zero byte, or some other well-defined character that both client and server agree means end-of-line, then your server can look for that character in its TCP input-stream and parse the preceding characters whenever it sees it. Commented Oct 29, 2018 at 22:01

3 Answers 3

5

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

  • send a fixed size prefix with the size of the expected message
  • use a self describing encoding (ber for example)
  • send a delimiter - say 2 blank lines in a text protocol (common for older RFC protocols)
Sign up to request clarification or add additional context in comments.

3 Comments

* send a delimiter like a (single) \n newline... nothing wrong with “older” rfc protocols, they rule the internet - http, for example
Plenty of old protocols still commonly in use (FTP, SMTP, POP3, IMAP, HTTP,etc) that use CRLF-delimited lines/commands
@RemyLebeau and barny , did not mean to demean those protocols.
3

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.

Comments

2

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, i.e., you could read two commands with a single network read (or one command and half of another command).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.