2011-02-12 40 views
1

我需要通過TCP傳輸一個8.3MB的文件,當我使用文件指針讀取和寫入文件時,我發送了大約8.6MB的數據,並且還收到8.6 MB通過計算send和recv調用的輸出,雖然我的文件大小是8.3 MB,此外當我通過查看它的屬性來單獨檢查文件的大小時,它大約是3-5 MB(隨每次傳輸而變化),但是當我使用文件描述符代替文件指針,然後我發送和recv恰好8.3 MB的數據和文件屬性大小也顯示8.3 MB。那麼在使用文件指針時會出現什麼問題?爲什麼在文件描述符中刪除它?但是如果我使用文件描述符,那麼我無法讀取我發送的文本文件。文本編輯器在文件中顯示一些二進制數據。我沒有得到一點正在發生的事情......請幫助,並在此先感謝使用文件指針通過TCP傳輸文件的問題

server.cpp

#include "server.h" 
void server() 
{ 
    int fd = open("out.txt",O_WRONLY); 
    struct sockaddr_in sin; 
    socklen_t addr_size; 
    char buf[MAX_LINE]; 
    int len; 
    int s, new_s; 
    /* build address data structure */ 
    bzero((char *) & sin, sizeof (sin)); 
    sin.sin_family = AF_INET; 
    sin.sin_addr.s_addr = INADDR_ANY; 
    sin.sin_port = htons(SERVER_PORT); 
    printf("File Descriptor : %d", fd); 
    /* setup passive open */ 
    if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) 
    { 
     perror("simplex-talk: socket"); 
     exit(1); 
    } 
    if ((bind(s, (struct sockaddr *) & sin, sizeof (sin))) < 0) 
    { 
     perror("simplex-talk: bind"); 
     exit(1); 
    } 
    listen(s, MAX_PENDING); 
    // wait for connection, then receive and print text */ 
    while (1) 
    { 
     if ((new_s = accept(s, (struct sockaddr *) & sin, &addr_size)) < 0) 
     { 
      perror("simplex-talk: accept"); 
      exit(1); 
     } 
     float total = 0; 
     printf("File Descriptor : %d", fd); 
     while (len = recv(new_s, buf, MAX_LINE, 0) && strcmp(buf,"close")) 
     { 
      buf[len] = 0; 
      total = total+len; 
      //write(stdout, buf, len); 
      write(fd, buf, len); 
      //printf("%fKB and %fMB\n",total/1024, total/(1024*1024)); 
     } 
     printf("File Descriptor : %d", fd); 
     close(new_s); 
    } 
} 

client.cpp

#include "client.h" 
void client(int argc, char** argv) 
{ 
    int fd = open("/home/nikku/Desktop/data.txt",O_RDONLY); 
    if (fd < 0) perror("File not opened\n"); 
    struct hostent *hp; 
    struct sockaddr_in sin; 
    char *host; 
    char buf[MAX_LINE]; 
    int s; 
    int len; 
    host = argv[1]; 
    if (argc == 2) 
    { 
     host = argv[1]; 
    } 
    else 
    { 
     fprintf(stderr, "usage: simplex-talk host\n"); 
     exit(1); 
    } 
    /* translate host name into peer’s IP address */ 
    gethostname(host,20); 
    printf("%s\n",host); 
    hp = gethostbyname(host); 
    if (!hp) 
    { 
     fprintf(stderr, "simplex-talk: unknown host: %s\n", host); 
     exit(1); 
    } 
    /* build address data structure */ 
    bzero((char *) & sin, sizeof (sin)); 
    sin.sin_family = AF_INET; 
    bcopy(hp->h_addr, (char *) & sin.sin_addr, hp->h_length); 
    sin.sin_port = htons(SERVER_PORT); 
    /* active open */ 
    if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) 
    { 
     perror("simplex-talk: socket"); 
     exit(1); 
    } 
    if (connect(s, (struct sockaddr *) & sin, sizeof (sin)) < 0) 
    { 
     perror("simplex-talk: connect"); 
     close(s); 
     exit(1); 
    } 
    printf("Connection Succeeded\n"); 
    /* main loop: get and send lines of text */ 
    float total = 0; 
    while (read(fd, buf, MAX_LINE)) 
    { 
     //usleep(1000);; 
     len = strlen(buf) + 1; 
     total += send(s, buf, len, 0); 
     //printf("%fKB and %fMB\n",total/1024, total/(1024*1024)); 
    } 
    send(s, "close", 6, 0); 
    close(fd); 
} 

只是如果我更換使用文件描述符與指針和使用fgets和fputs讀取和寫入然後我的文件傳輸不正確。但如果我使用文件描述符,那麼我無法讀取我發送的文本文件。文本編輯器在文件中顯示一些二進制數據。

+2

如果您的代碼有問題,那麼沒有人可以幫助您,除非他們能夠看到您的代碼。請發佈您的代碼。 –

回答

1
while (read(fd, buf, MAX_LINE)) 
{ 
    //usleep(1000);; 
    len = strlen(buf) + 1; 
    total += send(s, buf, len, 0); 
    //printf("%fKB and %fMB\n",total/1024, total/(1024*1024)); 
} 

這裏有一個問題,你有沒有保證,read將讀取一個零字節所以strlen(buf)是潛在的危險。還要注意,如果你設置了lenstrlen(buf) + 1,如果你設置了一個零字節,你將通過套接字發送它,但是如果你沒有讀取一個零字節,strlen將會在數組末尾讀取併發送'junk'穿過插座。

read的返回值存儲起來以便知道從fd實際讀取的字節數是明智的。

while (len = recv(new_s, buf, MAX_LINE, 0) && strcmp(buf,"close")) 
{ 
    buf[len] = 0; 
    total = total+len; 
    //write(stdout, buf, len); 
    write(fd, buf, len); 
    //printf("%fKB and %fMB\n",total/1024, total/(1024*1024)); 
} 

你的接收端似乎假定每次調用的recv將不包括零字節爲手動終止與0。注意緩衝區,你實際上沒有房間,如果recv實際上要做到這一點接收MAX_LINE字節,因爲buf只包含MAX_LINE個元素。由於您的寫作在任何情況下都受到長度限制,因此無需執行buf[len] = 0;

+0

謝謝!!!得到了問題 – chinmayaposwalia