下面是我在服务器端的方法:
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));
}
我这样调用方法:
send_err(socket_fd, "»»»»»»»»» illogical REQ request.\0");
。socket已经正确设置,
send_header
和send_args
方法正常工作。在服务器端,
printf
正常工作,但是strlen
没有返回正确的值(应该是33
,而不是43
)。在客户端,接收到的消息未定义:有时会出现奇怪的额外字符。显然,手动添加一个
\0
并未真正将NUL字符添加到我的字符串末尾。我该怎么做呢?我希望这个方法不需要人们计算发送的字符数,因为这很麻烦。
\0
字符,则应该拼写为nul
(1 el),如果它是空指针,则应该为NULL
/null
。 - Petr Skocik