提取文件内容并附加到套接字&发送套接字编程

时间:2015-05-30 02:25:58

标签: c sockets network-programming

将文件内容视为数据流(用户选择缓冲区大小)并通过套接字将其作为UDP数据包发送的正确/最简单方法是什么?然后记录发送的字节数? 什么是正确的方法来解决这个问题? 我的UDP客户端的当前进度。

#include <stdlib.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netdb.h>

#define PROTOPORT   33455 //Default Port Number
int main(int argc, char *argv[]) {

    char *dHost;            //Pointer to Destination IP Address
    int port;               //Integer to hold Port Number
    char *host;             //Pointer to Host IP Address
    char *inputFile;        //Pointer to Input File Name
    char *outputFile;       //Pointer to Output File Name
    int sDescriptor;         //Socket Descriptor

    struct hostent  *ptrh;   /* pointer to a host table entry   */
    struct  protoent *ptrp;  /* pointer to a protocol table entry   */
    struct sockaddr_in sad; /* structure to hold an IP address  */

    dHost = argv[1];        //Server IP Address
    host =  argv[3];        //Client IP Address
    inputFile = argv[4];    //Input File Name
    outputFile = argv[5];   //Output File Name
    char buffer[atoi(argv[6])];   //Buffer

    //Map Port
    if (argc > 2) {
        port = atoi(argv[2]);
    } else {
        port = PROTOPORT;
    }
    if (port > 0)
        sad.sin_port = htons((u_short)port);
    else {
        fprintf(stderr,"bad port number %s\n",argv[2]);
        exit(1);
    }

    //Map IP Address
    ptrh = gethostbyname(host);
    memcpy(&sad.sin_addr, ptrh->h_addr, ptrh->h_length);

    //Create UDP Socket
    sDescriptor = socket(PF_INET, SOCK_DGRAM, ptrp->p_proto);
    if (sDescriptor < 0) {
        fprintf(stderr, "Socket creation failed\n");
        exit(1);
    }

    //Connect
    if (connect(sDescriptor, (struct sockaddr *)&sad, sizeof(sad)) < 0) {
        fprintf(stderr,"Connect failed\n");
        exit(1);
    }

    //Send


    return 0;
}

1 个答案:

答案 0 :(得分:1)

尝试这样的事情:

#include <stdlib.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netdb.h>

#define PROTOPORT   33455 //Default Port Number
#define BUFSIZE     1024  //Default Buffer Size

int main(int argc, char *argv[]) {

    char *dHost;            //Pointer to Destination IP Address
    int  port;              //Integer to hold Port Number
    char *host;             //Pointer to Host IP Address
    char *inputFile;        //Pointer to Input File Name
    char *outputFile;       //Pointer to Output File Name
    int  sDescriptor;       //Socket Descriptor
    char *buffer;           //Buffer
    int  bufferSize;        //Buffer allocated size, in bytes
    int  readSize;          //Buffer used size, in bytes

    struct hostent *ptrh;   // pointer to a host table entry
    struct sockaddr_in sad; // structure to hold an IPv4 address

    FILE *inFile;

    if (argc < 6) {
        fprintf(stderr,"not enough parameters\n");
        exit(1);
    }

    dHost = argv[1];        //Server IP Address
    port = atoi(argv[2]);   //Port
    if (port <= 0) port = PROTOPORT;
    host = argv[3];         //Client IP Address
    inputFile = argv[4];    //Input File Name
    outputFile = argv[5];   //Output File Name
    bufferSize = atoi(argv[6]);
    if (bufferSize <= 0) bufferSize = BUFSIZE;

    memset(&sad, 0, sizeof(sad));
    sad.sin_family = AF_INET;
    sad.sin_port = htons((u_short)port);

    //Map IP Address

    sad.sin_addr.s_addr = inet_addr(dHost);
    if (sad.sin_addr.s_addr == INADDR_NONE) {
        ptrh = gethostbyname(dHost);
        if (ptrh == NULL) {
            fprintf(stderr,"cannot resolve host %s\n", dHost);
            exit(1);
        }
        if (ptrh->h_addrtype != AF_INET) {
            fprintf(stderr,"host %s does not resolve to IPv4 address\n", dHost);
            exit(1);
        }
        memcpy(&sad.sin_addr, ptrh->h_addr, ptrh->h_length);
    }

    //Create UDP Socket
    sDescriptor = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (sDescriptor < 0) {
        fprintf(stderr, "Socket creation failed\n");
        exit(1);
    }

    //Connect
    if (connect(sDescriptor, (struct sockaddr *)&sad, sizeof(sad)) < 0) {
        fprintf(stderr,"Connect failed\n");
        close(sDescriptor);
        exit(1);
    }

    inFile = fopen(inputFile, "rb");
    if (inFile == NULL) {
        fprintf(stderr,"Cannot open file %s\n", inputFile);
        close(sDescriptor);
        exit(1);
    }

    //Send

    buffer = malloc(bufferSize);
    if (buffer == NULL) {
        fprintf(stderr,"Cannot allocate buffer\n");
        fclose(inFile);
        close(sDescriptor);
        exit(1);
    }

    while (1) {
        readSize = fread(buffer, 1, bufferSize, inFile);
        if (readSize <= 0) {
            if (ferror(inFile) != 0) {
                fprintf(stderr,"Cannot read from file %s\n", inputFile);
                free(buffer);
                fclose(inFile);
                close(sDescriptor);
                exit(1);
            }
            break; 
        }

        if (send(sDescriptor, buffer, readSize, 0) < 0) {
            fprintf(stderr,"Send failed\n");
            free(buffer);
            fclose(inFile);
            close(sDescriptor);
            exit(1);
        }
    }

    free(buffer);
    fclose(inFile);
    close(sDescriptor);

    return 0;
}