2013-04-02 7 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 문을 살펴 봐야합니다. 첫째로, 당신은 RECV가 512 바이트를하지 않을 경우 중단 할 이유 while 루프 검사가> 0 초, 내가 이해하지 않기 때문에

fr_block_sz == 0 

결코 일어날 수있는 조건. (원한다면 LENGTH로 변경해야합니다.) 리턴 코드가 0 (소켓이 닫힌 경우) 또는 음수 (오류가있는 경우)가 될 때까지 계속 recv해야합니다. 스트림 소켓을 사용하면 수신자는 요청 된 각 IO에 대해 보낸 사람과 동일한 양의 데이터를 수신하지 않아도됩니다.

2

전송을 보장하지 않습니다, 그것은 더 적은을 보낼 수 있습니다에 당신이 그것을 물어 모든 데이터를 전송합니다. 보내기의 오류 만 확인하고 예상 한 것보다 적게 보내는 것은 아닙니다. recv는 같습니다. 따라서 구현의 양쪽에 동일한 버그가 있습니다. 커널 자체가 데이터를 전송하는 것에 대해 영리해질 수 있기 때문에 localhost를 사용하지 않을 수도 있습니다.

0

Joe는 자신의 대답에서 send이 항상 모든 데이터를 전송하지는 않는다고 지적했습니다. this guideline을 따라 send을 올바르게 사용할 수 있습니다.

MTU (실제 미디어에서 처리 할 수있는 최대 데이터 크기)을 치기 때문에 다른 컴퓨터의 서버로 보내려고 할 때만이 동작을 알 수 있습니다. 올바르게 기억한다면 루프백 주소를 사용할 때 MTU는 8K - 16K입니다. 바이너리 파일 크기가 실제 크기보다 작아서 실제 서버로 보내려고 한 후에 만이 파일 크기를 관찰 한 이유가 여기 있습니다. 반면 이더넷 매체에 대한 MTU는 비교적 낮은 1500 바이트입니다.

+0

입니다. 바이너리 파일 크기는 160.3K이며 get입니다. 전체 파일은 루프백 주소로 전송됩니다. 그러나 서버로 전송할 때 32603 바이트 만 전송되고 3419 바이트가 수신됩니다. –

+0

@ user2169037, 현재 코드를 사용하면 실제로 전송 된 바이트 수를 확인할 수 없습니다. file_에서 읽은 바이트 수만 인쇄하고 있습니다. 'send' 메커니즘을 [여기에 제안] (http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html#sendall)과 같이 바꿀 수 있습니까? –

+0

내가 제안한 "지침"문서에 따라 코드를 변경했지만 여전히 동일한 문제에 직면 해 있습니다. –