這是一個簡單執行RETR cmd其中服務器第一次收到文件名,然後它發送文件。c socket發送的文件中包含其他消息
/************************* 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),我不知道爲什麼我得到這個行爲烏
例子:
RETR tryfile.txt
File tryfile.txt received
cat tryfile.txt
「這是tryfile.txt,你好客戶
226文件trasferito CON SUCCESSO
221再見」
1)假設消息邊界由TCP保存。他們不是。 2)你假設NUL終止的字符串,但你不發送NUL。 3)IIRC,基於行的協議應該用'\ r \ n'來終止行。 – wildplasser 2012-07-08 13:27:52
1)呃?我不明白,sorrhy :(2)更正'發送'與'strlen(緩衝區)+ 1' 3)我假設使用正常線:)這是一個非常簡單的項目 – polslinux 2012-07-08 13:37:38
@wildplasser:雖然反直覺,絕對使用'\ r \ n'。雖然這是Windows的行結束,但我的經驗是,儘管Windows在接收UNIX行尾時抱怨並斷裂。但是,在標準的UNIX方式下,UNIX系統很樂意從DOS格式轉換成我,並沒有抱怨等。 – Linuxios 2012-07-08 13:38:36