gpt4 book ai didi

c套接字发送的文件包含其他消息

转载 作者:行者123 更新时间:2023-11-30 15:56:14 25 4
gpt4 key购买 nike

这是 RETR cmd 的简单实现,其中服务器首先接收文件名,然后发送文件。

/************************* RECEIVE FILE NAME AND SEND FILE *************************/
if(recv(newsockd, buffer, sizeof(buffer), 0) < 0){
perror("error receiving file name");
onexit(newsockd, sockd, 0, 2);
}
other = strtok(buffer, " ");
filename = strtok(NULL, "\n");
if(strcmp(other, "RETR") == 0){
printf("received RETR request\n");
} else onexit(newsockd, sockd, 0, 2);

fd = open(filename, O_RDONLY);
if (fd < 0) {
fprintf(stderr, "cannot open '%s': %s\n", filename, strerror(errno));
onexit(newsockd, sockd, 0, 2);
}

if(fstat(fd, &fileStat) < 0){
perror("Error fstat");
onexit(newsockd, sockd, fd, 3);
}
fsize = fileStat.st_size;
if(send(newsockd, &fsize, sizeof(fsize), 0) < 0){
perror("Error on sending file size\n");
onexit(newsockd, sockd, fd, 3);
}

rc = sendfile(newsockd, fd, &offset, fileStat.st_size);
if(rc == -1) {
fprintf(stderr, "error sending file: '%s'\n", strerror(errno));
onexit(newsockd, sockd, fd, 3);
}
if((uint32_t)rc != fsize) {
fprintf(stderr, "transfer incomplete: %d di %d bytes sent\n", rc, (int)fileStat.st_size);
onexit(newsockd, sockd, fd, 3);
}
memset(buffer, 0, sizeof(buffer));
strcpy(buffer, "226 File trasferito con successo\n\0");
if(send(newsockd, buffer, strlen(buffer)+1, 0) < 0){
perror("Errore durante l'invio 226");
onexit(newsockd, sockd, 0, 2);
}
memset(buffer, 0, sizeof(buffer));
strcpy(buffer, "221 Goodbye\n\0");
if(send(newsockd, buffer, strlen(buffer)+1, 0) < 0){
perror("Errore durante l'invio 221");
onexit(newsockd, sockd, 0, 2);
}
/************************* END PART *************************/

这是客户端程序的片段:

/************************* SEND FILE NAME AND RECEIVE FILE *************************/
printf("Inserire il nome del file da scaricare: ");
if(fgets(dirpath, BUFFGETS, stdin) == NULL){
perror("fgets name file");
close(sockd);
}
filename = strtok(dirpath, "\n");
sprintf(buffer, "RETR %s", dirpath);
if(send(sockd, buffer, strlen(buffer), 0) < 0){
perror("error sending file name");
close(sockd);
exit(1);
}
if(read(sockd, &fsize, sizeof(fsize)) < 0){
perror("error on receiving file size\n");
close(sockd);
exit(1);
}
fd = open(filename, O_CREAT | O_WRONLY, 0644);
if (fd < 0) {
perror("open");
exit(1);
}

while(((uint32_t)total_bytes_read != fsize) && ((nread = read(sockd, filebuffer, fsize)) > 0)){
if(write(fd, filebuffer, nread) < 0){
perror("write");
close(sockd);
exit(1);
}
total_bytes_read += nread;
}
memset(buffer, 0, sizeof(buffer));
if(recv(sockd, buffer, 34, 0) < 0){
perror("Error receiving 226");
close(sockd);
exit(1);
}
printf("%s", buffer);
memset(buffer, 0, sizeof(buffer));
if(recv(sockd, buffer, 13, 0) < 0){
perror("Error receiving 221");
close(sockd);
exit(1);
}
printf("%s", buffer);
memset(buffer, 0, sizeof(buffer));
close(fd);
/************************* END PART *************************/

问题是什么?
问题是已发送的文件还包含服务器发送的 2 条消息(226 和 221),我不知道为什么会出现这种行为 O.o
示例:
RETR tryfile.txt
收到文件 tryfile.txt
cat tryfile.txt
“这是 tryfile.txt,你好客户端
226 连续文件传输
221再见”

最佳答案

strlen() 计算\0,因此 strlen() 将返回 12:

strcpy(buffer, "221 Goodbye\n\0");
if(send(newsockd, buffer, strlen(buffer), 0) < 0){ ...}

客户端使用硬编码值 13。(与发送 33 和预期 34 的其他状态消息相同。)顺便说一句:您确实需要一些缓冲机制,至少在客户端中。

更新:要显示带有“嵌入 null”的字符串的 strlen:

#include <stdio.h>
#include <string.h>

int main(void)
{
fprintf(stderr, "strlen is %u\n", (unsigned) strlen("221 Goodbye\n\0") );

return 0;
}

说明:strlen() 仅计算字符数,直到遇到“\0”字符

关于c套接字发送的文件包含其他消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11383480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com