UDP服务器和客户端无法正常工作

时间:2015-10-31 16:42:42

标签: sockets udp server client

我正在开发一个代码来实现一个停止并等待将发送500个数据包的UDP服务器。但是,我只收到255个ACK而不是500个,而且无法找出原因。

客户代码:

#include "headsock.h"

#define TRUE 1
#define FALSE 0

float str_cli(FILE *fp, int sockfd, long *len, struct sockaddr *addr, int addrlen, socklen_t *len_recvfrom); // communication function
void tv_sub(struct timeval *out, struct timeval *in); //calculate the time interval between out and in

int main(int argc, char **argv)
{
    int sockfd;
    float ti, rt;
    long len;
    struct sockaddr_in ser_addr;
    char ** pptr;
    struct hostent *sh;
    struct in_addr **addrs;
    FILE *fp;
    socklen_t len_recvfrom;

    if (argc != 2)
    {
        printf("parameters not match");
        exit(0);
    }

    sh = gethostbyname(argv[1]); // get host's information
    if (sh == NULL) {
        printf("error when gethostby name");
        exit(0);
    }

    addrs = (struct in_addr **)sh->h_addr_list;
    printf("canonical name: %s\n", sh->h_name); // print the remote host's information
    for (pptr=sh->h_aliases; *pptr != NULL; pptr++)
        printf("the aliases name is: %s\n", *pptr);
    switch(sh->h_addrtype)
    {
        case AF_INET: // the address family that is used for the socket you're creating (in this case an Internet Protocol address)
            printf("AF_INET\n");
        break;
        default:
            printf("unknown addrtype\n");
        break;
    }

    sockfd = socket(AF_INET, SOCK_DGRAM, 0); // create the socket
    if (sockfd <0)
    {
        printf("error in socket");
        exit(1);
    }

    ser_addr.sin_family = AF_INET; // address format is host and port number                                              
    ser_addr.sin_port = htons(MYUDP_PORT);
    //copies count characters from the object pointed to by src to the object pointed to by dest
    memcpy(&(ser_addr.sin_addr.s_addr), *addrs, sizeof(struct in_addr));
    bzero(&(ser_addr.sin_zero), 8);

    if((fp = fopen ("myfile.txt","r+t")) == NULL)
    {
        printf("File doesn't exit\n");
        exit(0);
    }

    // perform the transmission and receiving
    ti = str_cli(fp, sockfd, &len, (struct sockaddr *)&ser_addr, sizeof(struct sockaddr_in), &len_recvfrom);

    rt = (len / (float)ti); // caculate the average transmission rate
    printf("Time(ms) : %.3f, Data sent(byte): %d\nData rate: %f (Kbytes/s)\n", ti, (int)len, rt);

    close(sockfd);
    fclose(fp);
    exit(0);
}

// transmission function
float str_cli(FILE *fp, int sockfd, long *len, struct sockaddr *addr, int addrlen, socklen_t *len_recvfrom)
{
    char *buf;
    long lsize, ci;
    char sends[DATALEN];
    struct ack_so ack;
    int n, slen;
    float time_inv = 0.0;
    struct timeval sendt, recvt;
    ci = 0;

    int prev_msg_acked = TRUE;
    int ack_number = 1;

    fseek(fp, 0, SEEK_END);
    lsize = ftell (fp);
    rewind(fp);
    printf("The file length is %d bytes\n", (int)lsize);
    printf("the packet length is %d bytes\n", DATALEN);

    // allocate memory to contain the whole file.
    buf = (char *) malloc(lsize);
    if (buf == NULL)
       exit (2);

    // copy the file into the buffer.
    // read lsize data elements, each 1 byte
    fread(buf, 1, lsize, fp);

    // the whole file is loaded in the buffer
    buf[lsize] ='\0'; // append the end byte
    gettimeofday(&sendt, NULL); // get the current time
    while(ci <= lsize)
    {
            if (prev_msg_acked) // only transmits when previous message has been acknowledged
        {
            if ((lsize+1-ci) <= DATALEN) // final string
                slen = lsize+1-ci;
            else // send message of length DATALEN
                slen = DATALEN;
            memcpy(sends, (buf+ci), slen);

            /*************** SEND MESSAGE ***************/
            if((n = sendto(sockfd, &sends, slen, 0, addr, addrlen)) == -1) {
                printf("send error!\n"); // send the data
                exit(1);
            }

            // update the expected ACK number
            if (ack_number == 1)
                ack_number = 0;
            else
                ack_number = 1;

            ci += slen;
        }

        /*************** RECEIVE ACK ***************/
        if ((n = recvfrom(sockfd, &ack, 2, 0, addr, &addrlen)) == -1)
        {
            printf("error when receiving\n");
            exit(1);
        }

        if (ack.num != ack_number || ack.len != slen) // ACK wrong
        {
            printf("%i %i expected\n", ack_number, slen);
            printf("%i %i received\n", ack.num, ack.len);
            printf("ACK check fails, retransmission...\n");
            prev_msg_acked = FALSE;
        }
        else
            prev_msg_acked = TRUE;
    }

    gettimeofday(&recvt, NULL);
    *len= ci; // get current time
    tv_sub(&recvt, &sendt); // get the whole trans time
    time_inv += (recvt.tv_sec)*1000.0 + (recvt.tv_usec)/1000.0;

    return(time_inv);
}

//calculate the time interval between out and in
void tv_sub(struct  timeval *out, struct timeval *in)
{
    if ((out->tv_usec -= in->tv_usec) <0)
    {
        --out ->tv_sec;
        out ->tv_usec += 1000000;
    }
    out->tv_sec -= in->tv_sec;
}

服务器代码:

#include "headsock.h"

void str_ser(int sockfd, int *ack_number); // transmitting and receiving function

int main(int argc, char *argv[])
{
    int sockfd;
    struct sockaddr_in my_addr;

    int ack_number = 1;

    //create socket
    if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
        printf("error in socket\n");
        exit(1);
    }

    my_addr.sin_family = AF_INET; // Address family; must be AF_INET
    my_addr.sin_port = htons(MYUDP_PORT); // Internet Protocol (IP) port.
    my_addr.sin_addr.s_addr = INADDR_ANY; // IP address in network byte order. INADDR_ANY is 0.0.0.0 meaning "all the addr"
    // places nbyte null bytes in the string s
    // this function will be used to set all the socket structures with null values
    bzero(&(my_addr.sin_zero), 8);

    // binds the socket to all available interfaces
    if (bind(sockfd, (struct sockaddr *) &my_addr, sizeof(struct sockaddr)) == -1) {
        printf("error in binding\n");
        perror("socket error");
        exit(1);
    }

    // receive and ACK
    str_ser(sockfd, &ack_number);

    close(sockfd);
    exit(0);
}

// transmitting and receiving function
void str_ser(int sockfd, int *ack_number)
{   
    FILE *fp;
    char buf[BUFSIZE];
    char recvs[DATALEN];
    int end = 0, n = 0;
    long lseek = 0;
    struct ack_so ack;

    struct sockaddr_in addr;
    socklen_t len = sizeof(struct sockaddr_in);

    printf("receiving data!\n");

    while(!end)
    {
        // receive the packet
        if ((n = recvfrom(sockfd, &recvs, DATALEN, 0, (struct sockaddr *)&addr, &len)) == -1)
        {
            printf("error when receiving\n");
            exit(1);
        }

        // toggle the ack_number
        if (*ack_number == 1)
            *ack_number = 0;
        else
            *ack_number = 1;

        // if the last bit of the received string is the EoF
        if (recvs[n-1] == '\0')
        {
            end = 1;
            n--;
        }

        memcpy((buf+lseek), recvs, n);
        lseek += n;

        // up to here, successfully received a packet
        // send ACK back
        ack.num = *ack_number;
        ack.len = n;
        printf("%i %i as ACK sent\n", ack.num, ack.len);
        if ((n = sendto(sockfd, &ack, 2, 0, (struct sockaddr *)&addr, len)) == -1)
        {
            printf("ACK send error!\n");
            exit(1);
        }
    }

    if ((fp = fopen ("myUDPreceive.txt", "wt")) == NULL)
    {
        printf("File doesn't exit\n");
        exit(0);
    }

    fwrite (buf, 1, lseek, fp); //write data into file
    fclose(fp);
    printf("A file has been successfully received!\nThe total data received is %d bytes\n", (int)lseek);
}

0 个答案:

没有答案