2013-04-02 160 views
0

當我使用下面的代碼傳輸二進制文件時,只有一半的文件正在傳輸。當我在使用同一個二進制文件的環回地址時檢查相同的代碼時,它可以正常工作,即傳輸整個文件。套接字程序文件傳輸

是否有下面的代碼或我的網絡什麼問題?

Client.c:

#include<stdlib.h> 
#include<stdio.h> 
#include<errno.h> 
#include<string.h> 
#include<sys/types.h> 
#include<netinet/in.h> 
#include<sys/wait.h> 
#include<sys/socket.h> 
#include<signal.h> 
#include<ctype.h>   
#include<arpa/inet.h> 
#include<netdb.h> 
#define PORT 20000 
#define LENGTH 512 

void error(const char *msg) 
{ 
    perror(msg); 
    exit(1); 
} 

int main(int argc, char *argv[]) 
{ 
    /* Variable Definition */ 
    int sockfd, nsockfd; 
    char revbuf[LENGTH]; 
    struct sockaddr_in remote_addr; 

    /* Get the Socket file descriptor */ 
    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { 
     fprintf(stderr, "ERROR: Failed to obtain Socket Descriptor! (errno = %d)\n",errno); 
     exit(1); 
    } 

    /* Fill the socket address struct */ 
    remote_addr.sin_family = AF_INET; 
    remote_addr.sin_port = htons(PORT); 
    inet_pton(AF_INET, "192.168.103.190", &remote_addr.sin_addr); 
    //inet_pton(AF_INET, "192.168.103.179", &remote_addr.sin_addr); 
    bzero(&(remote_addr.sin_zero), 8); 

    /* Try to connect the remote */ 
    if (connect(sockfd, (struct sockaddr *)&remote_addr, sizeof(struct sockaddr)) == -1) { 
     fprintf(stderr, "ERROR: Failed to connect to the host! (errno = %d)\n",errno); 
     exit(1); 
    } 
    else 
     printf("[Client] Connected to server at port %d...ok!\n", PORT); 

    /*send to server*/ 
    char* fs_name = "/usr/local/context.2848"; 
    char sdbuf[LENGTH]; 
    printf("[Client] Sending %s to the Server... ", fs_name); 
    FILE *fs = fopen(fs_name, "r"); 
    if(fs == NULL) { 
     printf("ERROR: File %s not found.\n", fs_name); 
     exit(1); 
    } 
    bzero(sdbuf, LENGTH); 
    int fs_block_sz,i=0; 
    while((fs_block_sz = fread(sdbuf, sizeof(char), LENGTH, fs)) > 0) { 
     printf("Data Sent %d = %d\n",i,fs_block_sz); 
     if(send(sockfd, sdbuf, fs_block_sz, 0) < 0) { 
      fprintf(stderr, "ERROR: Failed to send file %s. (errno = %d)\n", fs_name,  errno); 
      exit(1); 
     } 
     bzero(sdbuf, LENGTH); 
     i++; 
    } 
    close (sockfd); 
    printf("[Client] Connection lost.\n"); 
    return (0); 
} 

Server.c

#include<stdlib.h> 
#include<stdio.h> 
#include<errno.h> 
#include<string.h> 
#include<sys/types.h> 
#include<netinet/in.h> 
#include<sys/wait.h> 
#include<sys/socket.h> 
#include<signal.h> 
#include<ctype.h>   
#include<arpa/inet.h> 
#include<netdb.h> 
#define PORT 20000 
#define BACKLOG 5 
#define LENGTH 512 

void error(const char *msg) 
{ 
    perror(msg); 
    exit(1); 
} 

int main() 
{ 
    /* Defining Variables */ 
    int sockfd, nsockfd; 
    int num; 
    int sin_size; 
    struct sockaddr_in addr_local; /* client addr */ 
    struct sockaddr_in addr_remote; /* server addr */ 
    char revbuf[LENGTH]; // Receiver buffer 

    /* Get the Socket file descriptor */ 
    if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { 
     fprintf(stderr, "ERROR: Failed to obtain Socket Descriptor. (errno = %d)\n", errno); 
     exit(1); 
    } 
    else 
     printf("[Server] Obtaining socket descriptor successfully.\n"); 

    /* Fill the client socket address struct */ 
    addr_local.sin_family = AF_INET; // Protocol Family 
    addr_local.sin_port = htons(PORT); // Port number 
    addr_local.sin_addr.s_addr = INADDR_ANY; // AutoFill local address 
    bzero(&(addr_local.sin_zero), 8); // Flush the rest of struct 

    /* Bind a special Port */ 
    if(bind(sockfd, (struct sockaddr*)&addr_local, sizeof(struct sockaddr)) == -1) { 
     fprintf(stderr, "ERROR: Failed to bind Port. (errno = %d)\n", errno); 
     exit(1); 
    } 
    else 
     printf("[Server] Binded tcp port %d in addr 127.0.0.1 sucessfully.\n",PORT); 

    /* Listen remote connect/calling */ 
    if(listen(sockfd,BACKLOG) == -1) { 
     fprintf(stderr, "ERROR: Failed to listen Port. (errno = %d)\n", errno); 
     exit(1); 
    } 
    else 
     printf ("[Server] Listening the port %d successfully.\n", PORT); 

    int success = 0; 
    while(success == 0) 
    { 
     sin_size = sizeof(struct sockaddr_in); 

     /* Wait a connection, and obtain a new socket file despriptor for single connection */ 
     if ((nsockfd = accept(sockfd, (struct sockaddr *)&addr_remote, &sin_size)) == -1) { 
      fprintf(stderr, "ERROR: Obtaining new Socket Despcritor. (errno = %d)\n", errno); 
      exit(1); 
     } 
     else 
      printf("[Server] Server has got connected from %s.\n", inet_ntoa(addr_remote.sin_addr)); 
     /*Receive File from Client */ 
     char* fr_name = "/home/ankita/context.2848"; 
     FILE *fr = fopen(fr_name, "a"); 
     if(fr == NULL) 
      printf("File %s Cannot be opened file on server.\n", fr_name); 
     else { 
      bzero(revbuf, LENGTH); 
      int fr_block_sz = 0; 
      int i=0; 
      while((fr_block_sz = recv(nsockfd, revbuf, LENGTH, 0)) > 0) { 
       printf("Data Received %d = %d\n",i,fr_block_sz); 
       int write_sz = fwrite(revbuf, sizeof(char), fr_block_sz, fr); 
       if(write_sz < fr_block_sz) 
        error("File write failed on server.\n"); 
       bzero(revbuf, LENGTH); 
       i++; 
      } 
      if(fr_block_sz < 0) { 
       if (errno == EAGAIN) 
        printf("recv() timed out.\n"); 
       else { 
        fprintf(stderr, "recv() failed due to errno = %d\n", errno); 
        exit(1); 
       } 
      } 
      printf("Ok received from client!\n"); 
      fclose(fr); 
     } 
     success = 1; 
    } 
} 
+0

你是說你發送一個二進制文件,但你閱讀文本模式/寫 –

回答

0

你應該看看在recv的循環中的if語句。首先,這

fr_block_sz == 0 

不可能發生,因爲while循環檢查> 0。其次,我不明白你爲什麼要分手,如果recv的沒有得到512個字節的條件。 (如果你真的想把它改成LENGTH)。你應該繼續recv直到返回碼是0(在這種情況下套接字是關閉的)或者是否定的(在這種情況下你有一個錯誤)。對於流套接字,接收方不一定會收到發送方爲每個IO請求發送的相同數量的數據。

2

發送並不能保證把所有你問它的數據,它可能會少發。你只檢查發送錯誤,而不是發送比你期望的更少。 recv是一樣的。所以你在實現的兩邊都有相同的錯誤。因爲內核可以聰明地將數據傳輸給自己,所以你可能會逃避本地主機。

0

喬在他的回答中指出,send並不總是發送所有的數據。您可以按照this guideline正確使用send

只有當您嘗試發送到另一臺計算機上的服務器時,您纔會注意到此行爲,因爲您正在擊中MTU(即物理介質可以處理的最大數據大小)。如果我沒有記錯,當使用環回地址時,MTU是8K-16K。我猜你的二進制文件大小恰好小於這個,這就是爲什麼你只有在試圖發送到真正的服務器後才觀察到這一點。而以太網介質的MTU是1500字節,相對要低得多。

+0

很好,我的二進制文件大小是160.3K,它是正如我所說的get.The整個文件轉移環回地址。但是,當傳輸到服務器時,只傳輸32603個字節並接收到3419個字節。 –

+0

@ user2169037,使用您當前的代碼,您無法確定實際發送的字節數。您只打印從file_讀取的字節數。你能否把你的'send'機制​​改成[這裏建議](http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html#sendall)? –

+0

我已根據您建議的「指南」文檔更改我的代碼,但仍面臨同樣的問題。 –