2012-05-21 57 views
6

我正在嘗試Linux上的TCP文件傳輸。建立連接後,服務器應向客戶端發送「send.txt」,客戶端接收該文件並將其保存爲「receive.txt」。然後連接中斷。在Linux上使用TCP進行文件傳輸

正確的輸入和輸出應該是:

服務器端:

$./server & 
[server] obtain socket descriptor successfully. 
[server] bind tcp port 5000 in addr 0.0.0.0 successfully. 
[server] listening the port 5000 successfully. 
[server] server has got connect from 127.0.0.1. 
[server] send send.txt to the client…ok! 
[server] connection closed. 

客戶端:

$./client 
[client] connected to server at port 5000…ok! 
[client] receive file sent by server to receive.txt…ok! 
[client] connection lost. 

而且服務器和客戶端應該在處理後退出。

但是我現在已經有了給

$ ./server & 
[server] obtain socket descriptor successfully. 
[server] bind tcp port 5000 in addr 0.0.0.0 sucessfully. 
[server] listening the port 5000 sucessfully. 
[server] server has got connect from 127.0.0.1. 
[server] send send.txt to the client...ok! 
[server] connection closed. 

/*Here the server doesn't exit*/ 

$ ./client 
[client] connected to server at port 5000...ok! 

/*Here the client doesn't exit*/ 

另外,一個空的 「receive.txt」 產生。

我的代碼最初是爲了傳輸簡單的字符串而編寫的,它的工作是正確的。所以我想問題在於文件傳輸部分。

我的代碼如下:

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> 
#define PORT 5000 // The port which is communicate with server 
#define BACKLOG 10 
#define LENGTH 512 // Buffer length 
int main() 
{ 
    int sockfd; // Socket file descriptor 
    int nsockfd; // New Socket file descriptor 
    int num; 
    int sin_size; // to store struct size 
    struct sockaddr_in addr_local; 
    struct sockaddr_in addr_remote; 
    /* Get the Socket file descriptor */ 
    if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) 
    { 
     printf ("ERROR: Failed to obtain Socket Descriptor.\n"); 
     return (0); 
    } 
    else printf ("[server] obtain socket descriptor successfully.\n"); 
    /* Fill the local 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) 
    { 
     printf ("ERROR: Failed to bind Port %d.\n",PORT); 
     return (0); 
    } 
    else printf("[server] bind tcp port %d in addr 0.0.0.0 sucessfully.\n",PORT); 
    /* Listen remote connect/calling */ 
    if(listen(sockfd,BACKLOG) == -1) 
    { 
     printf ("ERROR: Failed to listen Port %d.\n", PORT); 
     return (0); 
    } 
    else printf ("[server] listening the port %d sucessfully.\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) 
      printf ("ERROR: Obtain new Socket Despcritor error.\n"); 
     else printf ("[server] server has got connect from %s.\n", inet_ntoa(addr_remote.sin_addr)); 
     /* Child process */ 
     if(!fork()) 
     { 
      char* f_name = "send.txt"; 
      char sdbuf[LENGTH]; // Send buffer 
      printf("[server] send %s to the client...", f_name); 
      FILE *fp = fopen(f_name, "r"); 
      if(fp == NULL) 
      { 
       printf("ERROR: File %s not found.\n", f_name); 
       exit(1); 
      } 
      bzero(sdbuf, LENGTH); 
      int f_block_sz; 
      while((f_block_sz = fread(sdbuf, sizeof(char), LENGTH, fp))>0) 
      { 
       if(send(nsockfd, sdbuf, f_block_sz, 0) < 0) 
       { 
        printf("ERROR: Failed to send file %s.\n", f_name); 
        break; 
       } 
       bzero(sdbuf, LENGTH); 
      } 
      printf("ok!\n"); 
      success = 1; 
      close(nsockfd); 
      printf("[server] connection closed.\n"); 
      while(waitpid(-1, NULL, WNOHANG) > 0); 
     } 
    } 
} 

client.c

#include <stdio.h> 
#include <stdlib.h> 
#include <errno.h> 
#include <string.h> 
#include <netdb.h> 
#include <sys/types.h> 
#include <netinet/in.h> 
#include <sys/socket.h> 
#define PORT 5000 
#define LENGTH 512 // Buffer length 
int main(int argc, char *argv[]) 
{ 
    int sockfd; // Socket file descriptor 
    char revbuf[LENGTH]; // Receiver buffer 
    struct sockaddr_in remote_addr; 
    /* Get the Socket file descriptor */ 
    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) 
    { 
     printf("ERROR: Failed to obtain Socket Descriptor!\n"); 
     return (0); 
    } 
    /* Fill the socket address struct */ 
    remote_addr.sin_family = AF_INET; 
    remote_addr.sin_port = htons(PORT); 
    inet_pton(AF_INET, "127.0.0.1", &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) 
    { 
     printf ("ERROR: Failed to connect to the host!\n"); 
     return (0); 
    } 
    else printf("[client] connected to server at port %d...ok!\n", PORT); 
    //printf ("OK: Have connected to %s\n",argv[1]); 
    printf("[client] receive file sent by server to receive.txt..."); 
    char* f_name = "receive.txt"; 
    FILE *fp = fopen(f_name, "a"); 
    if(fp == NULL) printf("File %s cannot be opened.\n", f_name); 
    else 
    { 
     bzero(revbuf, LENGTH); 
     int f_block_sz = 0; 
     int success = 0; 
     while(success == 0) 
     { 
      while(f_block_sz = recv(sockfd, revbuf, LENGTH, 0)) 
      { 
       if(f_block_sz < 0) 
       { 
        printf("Receive file error.\n"); 
        break; 
       } 
       int write_sz = fwrite(revbuf, sizeof(char), f_block_sz, fp); 
       if(write_sz < f_block_sz) 
       { 
        printf("File write failed.\n"); 
        break; 
       } 
       bzero(revbuf, LENGTH); 
      } 
      printf("ok!\n"); 
      success = 1; 
      fclose(fp); 
     } 
    } 
    close (sockfd); 
    printf("[client] connection lost.\n"); 
    return (0); 
} 

非常感謝您!

+2

的[發送,並用C/C++(GCC/G ++)接收在插座編程一個文件在Linux中(可能的複製http://stackoverflow.com/questions/2014033/send-and-receive-a -file-in-socket-programming-in-linux-with-cc-gcc -g) –

回答

4

您需要將代碼添加到f_block_sz代碼中。 recv()有幾個更多鈔票返回值:

  • <0 - 錯誤,錯誤數在errnoerrno.h
  • 0 - 連接關閉
  • >0 - 數據讀,字節數

您需要處理第二種情況。添加這種else情況:

else if(f_block_sz) 
{ 
    break; 
} 

因此,當服務器關閉連接,你的代碼將打印您[client] connection lost和退出循環將被打破。

+0

謝謝。我應該早點認識到這一點! – goldfrapp04

3

你還有一個問題,就是你的服務器程序是多進程的,並且每次獲得傳入連接時都會分叉。父母保持活動狀態以接受新的連接,並且孩子處理連接。從子進程退出還不足以導致父進程退出。

如果您只想處理一個連接,則不要使用分叉。

如果您希望繼續您的當前設置,那麼你將需要更改子進程使用_exit代替exit和獲取父進程來處理SIGCHLD信號(接收當一個子進程退出)。

即。

#include <signal.h> 

void terminate(int signal) { 
    // close sockfd -- you will need to make it global 
    // or have terminate alter some global variable that main can monitor to 
    // detect when it is meant to exit 
    exit(0); // don't exit if you choose the second option 
} 

int main() { 

    signal(SIGCHLD, terminate); 

    ... 

} 
+0

謝謝。我不應該從一開始就使用子進程......這是因爲我的原始程序是爲了讓服務器保持活着狀態。 – goldfrapp04