Skip to content

Commit

Permalink
communications.c: Added support for messages spanning multiple lines.
Browse files Browse the repository at this point in the history
Changed receive_command() to read from the socket until the second occurrence  of '\0' instead of waiting for the first \n, which previously made multi-line messages impossible. Now, messages containing '\0' will be truncated.

I would recommend to change the way this function reads from the socket; instead of reading one byte at a time, make the second and third byte sent to the socket by the server to be the size of the message. Then, the client determines the size it will need to parse and then either loop until the whole message has been read or read the entire message at once. I don't know enough C to implement this, but it would prevent from making so many read() calls as well as checking if len == sizeof(cmd) as many times as the size of the message.
  • Loading branch information
kit-la authored Aug 3, 2024
1 parent cf6164d commit 8c06ada
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions communications.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ receive_command(void)
}

if (read(cl, &c, 1) == 1) {
if (c == '\n') {
cmd[len++] = '\0';
if ((c == '\0') && (len > 0)) {
cmd[len - 1] = '\0';
break;
}
cmd[len++] = c;
Expand Down

0 comments on commit 8c06ada

Please sign in to comment.