Reputation: 5269
Here is the method on my server-side:
void send_err(int socket_fd, char *msg) {
/// To send an error message (`ERR`).
size_t len = strlen(msg);
printf("send_err: msg='%s' of size:%zu\n", msg, len);
INIT_HEAD_S(head_s, ERR, len);
send_header(socket_fd, &head_s, sizeof(cmd_header_t));
send_msg(socket_fd, msg, len * sizeof(char));
}
I call the method this way: send_err(socket_fd, "»»»»»»»»» illogical REQ request.\0");
The socket is set up properly, and the send_header
and send_args
methods work fine.
On the server-side, the printf
works just fine, but the strlen
does not return the proper value (it should be 33
, but it's giving out 43
instead).
On the client-side, the message received is undefined: some times there are weird extra characters appearing. Apparently, manually adding a \0
doesn't seem to actually add a NUL character at the end of my String.
How am I supposed to do this? I want the method to not require the person to count how many characters they are sending since it's annoying to do so.
Upvotes: 4
Views: 84
Reputation: 5269
void send_err(int socket_fd, char *msg) {
/// To send an error message (`ERR`).
size_t len = strlen(msg)+1; // because `strlen` doesn't include the NUL char
printf("send_err: msg='%s' of size:%zu\n", msg, len);
INIT_HEAD_S(head_s, ERR, len);
send_header(socket_fd, &head_s, sizeof(cmd_header_t));
send_msg(socket_fd, msg, len * sizeof(char));
}
As Alex F suggested in the comments, my error was with the manipulation of strlen
. I simply needed to add a +1
.
Upvotes: 4