C - TCP文件传输

时间:2014-10-17 14:53:18

标签: c tcp

我试图通过服务器 - 客户端关系在TCP上进行文件传输。但是我遇到了麻烦,因为每次服务器收到客户端请求的文件时,它都会在最后用垃圾打印出所请求的文件名,然后继续产生分段错误。此外,每当我将信息返回给客户端时,文档中的文本以某种方式管理作为标题的一部分。我已将下面的代码粘贴到我的服务器和客户端

Server.c

/*code for server portion of server-client setup*/
/*communication occurs overs TCP*/

#include <stdio.h>      /* for printf() and fprintf() */
#include <sys/socket.h> /* for socket(), bind(), and connect() */
#include <arpa/inet.h>  /* for sockaddr_in and inet_ntoa() */
#include <stdlib.h>     /* for atoi() and exit() */
#include <string.h>     /* for memset() */
#include <unistd.h>     /* for close() */
#include <errno.h>  /* for error numbers */
#include <dirent.h> /* for directories */

#define MAXPENDING 5    /* Maximum outstanding connection requests */
#define MAXLEN 255      /* Longest string  */

void DieWithError(char *errorMessage);  /* Error handling function */

int main(int argc, char *argv[])
{
    int servSock;                       /* Socket descriptor for server */
    int clntSock;                       /* Socket descriptor for client */
    struct sockaddr_in echoServAddr;    /* Local address */
    struct sockaddr_in echoClntAddr;    /* Client address */
    unsigned short echoServPort;        /* Server port */
    unsigned int clientOption, clntLen; /* Length of client address data structure */
    char data_recv[MAXLEN+1];       /* data received from client */
    int recvMsgSize;                /* size of message received */
    char data_sent[2000];       /* data sent to client */
    char file_request[50];      /* file requested by client */
    char file[2000];            /* last file sent to client */
    socklen_t len;  


    if (argc != 2)     /* Test for correct number of arguments */
    {
        fprintf(stderr, "Usage:  %s <Server Port>\n", argv[0]);
        exit(1);
    }

    echoServPort = atoi(argv[1]);  /* First arg:  local port */

    /* Create socket for incoming connections */
    if ((servSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
        DieWithError("socket() failed");

    /* Construct local address structure */
    memset(&echoServAddr, 0, sizeof(echoServAddr));   /* Zero out structure */
    echoServAddr.sin_family = AF_INET;                /* Internet address family */
    echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */
    echoServAddr.sin_port = htons(echoServPort);      /* Local port */

    /* Bind to the local address */
    if (bind(servSock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
        DieWithError("bind() failed");

    /* Mark the socket so it will listen for incoming connections */
    if (listen(servSock, MAXPENDING) < 0)
        DieWithError("listen() failed");

    /* Wait for a client to connect */
     if ((clntSock = accept(servSock, (struct sockaddr *) &echoClntAddr, 
                               &clntLen)) < 0)
            DieWithError("accept() failed");

    /* Run Forever */
    while(atoi(data_recv) != -1){            
    /* Block until receive message from a client */
    if(recvMsgSize = recv(clntSock, data_recv, sizeof(data_recv), 0) < 0)
    {
        printf("Could not block socket. Failed with : %s\n", strerror(errno));
    }
    printf("Handling client %s\n", inet_ntoa(echoClntAddr.sin_addr));       
    printf("%d\n", atoi(data_recv));
    recvMsgSize = 1000;
    printf("%d\n", atoi(data_recv));
    char temp[255];

    switch(atoi(data_recv)){
        printf("%d\n", atoi(data_recv));
            case 1:
            printf("Client has requested a list of all files\n");
                DIR  * d;
                struct dirent * dp;
                d = opendir(".");               



                if(d != NULL){
                    while((dp = readdir(d)) != NULL){
                        if(dp->d_type == DT_REG){
                            strcat(temp, dp->d_name);
                            strcat(temp, "\n");
                        }
                    }
                    strcpy(data_recv, temp);
                    strcpy(temp, "");

                        if(send(clntSock, data_recv, recvMsgSize, 0) < 0){
                            printf("send() sent a different number of bytes than expected");
                        }


                    closedir(d);
                }
                break;
            case 2:
                printf("Client has requested a single file. Waiting for file name.\n");
                    /* block until receive message from a client */

                if(recvMsgSize = recv(clntSock, file_request, sizeof(file_request), 0) < 0)
                {
                    printf("Could not block socket\n");
                    printf("Failed with the following error: %s\n", strerror(errno));
                }

                printf("Client has request: %s\n", file_request);

                FILE *fp;
                fp = fopen(file_request, "r");
                if(fp == NULL)
                {
                    // send message to client that file does not exist
                    printf("Specified file does not exist.");
                }

                fseek(fp, 0, SEEK_END);
                size_t file_size = ftell(fp);
                fseek(fp, 0, SEEK_SET);

                    if(fread(file, file_size, 1, fp) <= 0)
                {
                    // send message that unable to copy file to buffer
                    printf("Unable to copy file to buffer");
                    exit(1);
                    }

                if (send(clntSock, file, sizeof(file), 0) < 0)
                    {
                    printf("error in sending file");
                }

                bzero(file, sizeof(file));
                break;
            case 3:
                break;
            case 4:
                printf("Client has closed connection. Waiting on new clients");
                break;
        }
        recvMsgSize = 0;
        data_recv[0] = 0;
    }

    close(servSock);
}

Client.c

/*code for client portion of server-client setup*/
/*communication occurs overs TCP*/

#include <stdio.h>              /* for printf() and fprintf() */
#include <sys/socket.h>     /* for socket(), connect(), sendto(), and recvfrom() */
#include <arpa/inet.h>      /* for sockaddr_in and inet_addr() */
#include <stdlib.h>             /* for atoi() and exit() */
#include <string.h>             /* for memset() */
#include <unistd.h>             /* for close() */
#include <stdbool.h>        /* for boolean values */

#define RCVBUFSIZE 255   /* Size of receive buffer */

void DieWithError(char *errorMessage);  /* Error handling function */
char * printMenu();

int main(int argc, char *argv[])
{
    int sock,i,num;                  /* Socket descriptor */
    struct sockaddr_in echoServAddr; /* Echo server address */
    struct sockaddr_in fromAddr;     /* address of data sender */
    unsigned short echoServPort;     /* Echo server port */
    char *servIP;                    /* Server IP address (dotted quad) */
    char *echoString;                /* String to send to echo server */
    char echoBuffer[RCVBUFSIZE];     /* Buffer for echo string */
    unsigned int echoStringLen;      /* Length of string to echo */
    int bytesRcvd, totalBytesRcvd;   /* Bytes read in single recv() 
                                        and total bytes read */
    char menuOption[1];              /* User Menu seclection to send to server */
    char buffer[RCVBUFSIZE+1];       /* Buffer for receiving data from server */
    int recvDataLen;                 /* Length of received response */
    char * menuMsg;          /* used to print menu options */
    char * temp[255] = {0};
    char file_buffer[2000];      /* file sent by server */
    char file_request[50];       /* file requested by client */


    if (argc != 3)    /* Test for correct number of arguments */
    {
        fprintf(stderr,"Usage: %s <Server IP> [<Port Id>]\n", argv[0]);
        exit(1);
    }

    servIP = argv[1];           /* First arg: server IP address (dotted quad) */

    if (argc == 3)
    {
        echoServPort = atoi(argv[2]);  /* Use given port, if any */
    }
    else
    {
    fprintf(stderr,"Usage: %s requires [<Port Id>]\n", argv[0]);
    }

    /* Create a reliable, stream socket using TCP */
    if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
        DieWithError("socket() failed");

    /* Construct the server address structure */
    memset(&echoServAddr, 0, sizeof(echoServAddr));     /* Zero out structure */
    echoServAddr.sin_family      = AF_INET;             /* Internet address family */
    echoServAddr.sin_addr.s_addr = inet_addr(servIP);   /* Server IP address */
    echoServAddr.sin_port        = htons(echoServPort); /* Server port */

    /* Establish the connection to the echo server */
    if (connect(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
        DieWithError("connect() failed");

    //echoStringLen = strlen(echoString);          /* Determine input length */

    menuMsg = printMenu();

    while(atoi(menuOption) != -1){
    printf("%s", menuMsg);
    scanf("%s", &menuOption[0]);

    switch(atoi(menuOption)){
            case 1:
                /* send menu choice to server indicating client wants list of all files on server */
                if (send(sock, menuOption, sizeof(menuOption), 0) < 0){
                    printf("sendto() sent a different number of bytes than expected");
                }

                printf("\nYou choose to recieve a list of files from the server\n\n");
                printf("Below is the returned response from the server:\n");

                /* receive a response from the server */
                if ((bytesRcvd = recv(sock, buffer, RCVBUFSIZE - 1, 0)) <= 0)
                            DieWithError("recv() failed or connection closed prematurely");


                /* null terminate the received data */
                buffer[bytesRcvd] = '\0';
                printf("%s\n", buffer);    /* Print the echoed arg */

                break;
            case 2:
                /* send menu choice to server indicating client wants list of all files on server */
                if (send(sock, menuOption, sizeof(menuOption), 0) < 0){
                    printf("sendto() sent a different number of bytes than expected");
                }

                printf("You choose to retrieve a single file from the server\n");
                printf("What file would you like: ");
                scanf("%s", &file_request[0]);

                if (send(sock, file_request, strlen(file_request), 0) < 0)
                {
                        printf("sendto() sent a different number of bytes than expected");
                }

                /* Recv a response */
                    bytesRcvd = sizeof(fromAddr);
                if (recvDataLen = recv(sock, buffer, sizeof(buffer), 0) < 0)
                {
                        printf("recvfrom() failed\n");
                }

                char new_file[] = "copied";
                strcat(new_file, buffer);
                FILE *fp;
                fp = fopen(new_file, "w+");

                if(fwrite(buffer, 1, sizeof(buffer), fp) < 0)
                {
                    printf("error writting file\n");
                    exit(1);
                }
                break;
            case 3:
                    printf("You choose to retrieve all files from the server\n");
                break;
            case 4:
                /* send menu choice to server indicating client wants list of all files on server */
                if (send(sock, menuOption, sizeof(menuOption), 0) < 0){
                    printf("sendto() sent a different number of bytes than expected");
                }

                printf("Closing connection\n");
                close(sock);
                exit(0);
            default:
                printf("You picked an invalid option. Try again.\n");
                break;
        }

    }

    printf("\n");    /* Print a final linefeed */

    close(sock);
    exit(0);
}


char * printMenu()
{   
    static char message[250] = "Select an option from below:\n";
        strcat(message, "(1) List all files on server\n");
    strcat(message, "(2) Retrieve file from server\n");
    strcat(message, "(3) Retrieve all files from server\n");
    strcat(message, "(4) Close Connection\n");
    strcat(message, "Enter your selection: ");
    return message;
}

1 个答案:

答案 0 :(得分:2)

您没有将空终结符发送到服务器。

if (send(sock, file_request, strlen(file_request), 0) < 0)
{
     printf("sendto() sent a different number of bytes than   expected");
}

strlen不将空终止符计为长度。

将其替换为strlen(file_request) + 1

我并不是说这是一个完美的解决方案,只是一种指出错误的方法。