2

I am trying to implement a simple protocol by reading messages coming from a server and send messages back accordingly. I basically have a while-loop that at the moment looks like this

while(recv(sock, buffer, BUF) > 0 /* more stuff */) {
/* Handle message */
}

However, this gives me the whole message sent by the server until it's finished or the length specified by BUF is reached.
It would be much easier to be able to read it line-by-line and not like this, since sometimes the server sends several lines at once. Of course it is easy to implement a function that splits up a string in its lines, but I rather want a function that I can replace the recv(...) with that gives me all the lines. How can I realize something like that?

Staki42
  • 125
  • 1
  • 4
    Possible duplicate of [c recv() read until newline occurs](http://stackoverflow.com/questions/6090594/c-recv-read-until-newline-occurs) – Byte Lab Dec 09 '16 at 02:34
  • The simple way is to receive one character at a time, the efficient way is process all the lines in the buffer (also remembering the buffer may end halfway through a line) – user253751 Dec 09 '16 at 03:02
  • 1
    You may want to consider `fdopen`. – n. m. could be an AI Dec 09 '16 at 09:02
  • You wrote first _It would be much easier to be able to read it line-by-line_, but then _I rather want a function … that gives me all the lines._ Now how **do** you want it: line-by-line, or all lines at once? – Armali Jun 15 '18 at 14:21

0 Answers0