我在循环中有一个 TCP send() 命令。我注意到我发送的每条消息都只是附加到先前发送的消息的末尾。我不知道为什么。我已经尝试过 memset 和 bzero 来清除 my char *
,但我得到了相同的结果。有人知道为什么吗?
char *lsp = malloc(128);
for (i=0; i<3; i++) {
memset (lsp, 0, 128); // also tried bzero here
lsp_builder(lsp, this_router->label, routing_table, num_hosts-1, ++seq);
fprintf (logfd, "\tCreated the following lsp: %s\n\n", lsp); //<--looks great
for (i=0; i<6; i++) {
send (sockfd[i], lsp, strlen(lsp), 0);
}
}
然后,在接收端,我有这个代码:
char incoming_lsp[128];
bzero(incoming_lsp, sizeof(incoming_lsp));
recv(newfd[i], &incoming_lsp, sizeof(incoming_lsp), 0);
// this is where I can see the data being appended
fprintf(logfd, "\tReceived: %s\n", incoming_lsp);