c socket发送文件包含其他消息

时间:2012-07-08 13:21:46

标签: c sockets sendfile

这是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
File tryfile.txt received
cat tryfile.txt
“这是tryfile.txt,你好客户端
226 File trasferito con successo
221再见“

2 个答案:

答案 0 :(得分:1)

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

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

客户端使用硬编码值13.(对于其他状态消息,其中33发送,34预期。)BTW:您真的需要一些缓冲机制,至少在客户端。

更新: 要显示带有“嵌入空值”的字符串的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'字符

答案 1 :(得分:0)

我发现并(最终)解决了这个问题 这是解决方案:

uint32_t fsize_tmp = fsize;
while(((uint32_t)total_bytes_read != fsize) && ((nread = read(sockd, filebuffer, fsize_tmp)) > 0)){
     if(write(fd, filebuffer, nread) < 0){
    perror("write");
    close(sockd);
    exit(1);
      }
      total_bytes_read += nread;
      fsize_tmp -= nread;
}

问题是由于我没有检查“动态”文件大小的事实(如果文件以75%发送我的while循环预期fsize再次这是不可能的)所以使用此代码我将减小文件大小每次需要时:)