在最简单的UDP套接字编程中确实会丢失ACK吗?

时间:2013-10-29 04:29:10

标签: c sockets

据我所知,与TCP不同,UDP有点不可靠。当我学习UDP时,我尝试实现一个停止等待流控制协议。这是一种非常简单的通信:在一台PC上运行的客户端和在另一台PC上运行的服务器。我试图将我的.txt文件从客户端传输到服务器。

首先,我计划首先实施“无错误”版本,然后然后添加错误概率目的,看看停止和等待机制是如何工作的。

我刚刚完成了无错误版本的实现,并且惊人地发现在我所谓的“无错误”版本中,确实存在ACK丢失的情况

所以即使在我手动添加错误概率之前,ACK也已经丢失(奇怪的是它永远不会被损坏)。

由于这是一个非常简单的通信,我预计没有ACK丢失或损坏。我想知道我是否在某个地方实现了错误,或者它应该是这样的?

我的代码

cli.c

#include "headsock.h"

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 ("s.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-1) / (float)ti); // caculate the average transmission rate
    printf("Time(ms) : %.3f, Data sent(byte): %d\nData rate: %f (Kbytes/s)\n", ti, (int)len-1, rt);

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

// communication function
float str_cli(FILE *fp, int sockfd, long *len, struct sockaddr *addr, int addrlen, socklen_t *len_recvfrom)
{
    char *buf;
    long lsize, ci;
    struct pack_so packet;
    struct ack_so ack;
    int n;
    float time_inv = 0.0;
    struct timeval sendt, recvt;
    struct timeval sendTime, curTime;
    ci = 0;

    int prev_msg_acked = TRUE;
    int next_packet_num = 0;

    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", PACKLEN);

    // allocate memory to contain the whole file.
    buf = (char *) malloc(lsize+1);
    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
        {
            // form the packet to transmit
            if ((lsize-ci+1) <= PACKLEN) // final string
                packet.len = lsize-ci+1;
            else // send message of length PACKLEN
                packet.len = PACKLEN;
            packet.num = next_packet_num;
            memcpy(packet.data, (buf+ci), packet.len);

            /*************** SEND MESSAGE ***************/
            gettimeofday(&sendTime, NULL);
            if((n = sendto(sockfd, &packet, sizeof(packet), 0, addr, addrlen)) == -1)
            {
                printf("Send error!\n"); // send the data
                exit(1);
            }

            // update the sequence number
            if (packet.num == 1)
                next_packet_num = 0;
            else
                next_packet_num = 1;

            ci += packet.len;

            prev_msg_acked = FALSE;
        }

        /*************** RECEIVE ACK ***************/
        // MSG_DONTWAIT flag, non-blocking
        // receives nothing
        if ((n = recvfrom(sockfd, &ack, sizeof(ack), MSG_DONTWAIT, addr, len_recvfrom)) == -1)
        {
            // monitors how long nothing is received
            gettimeofday(&curTime, NULL);
            // if timeout
            if (curTime.tv_sec - sendTime.tv_sec > TIMEOUT)
            {
                // retransmit
                printf("Timeout! Resend this.\n");
                /*************** RESEND MESSAGE ***************/
                gettimeofday(&sendTime, NULL);
                if((n = sendto(sockfd, &packet, sizeof(packet), 0, addr, addrlen)) == -1)
                {
                    printf("Send error!\n"); // send the data
                    exit(1);
                }
            }
        }

        // An ACK is received
        else
        {
            printf("ACK received. ");
            // if what the server expects next is this one or server receives a different length
            // if ACK is incorrect
            if (ack.num != next_packet_num || ack.len != packet.len)
            {
                printf("Incorrect. Resend this. ");
                printf("(%i %i expected, but %i %i received)\n", next_packet_num, packet.len, ack.num, ack.len);

                /*************** RESEND MESSAGE ***************/
                gettimeofday(&sendTime, NULL);
                if((n = sendto(sockfd, &packet, sizeof(packet), 0, addr, addrlen)) == -1)
                {
                    printf("Send error!\n"); // send the data
                    exit(1);
                }
            }
            // if ACK correct
            else
            {
                printf("Correct. Send next.\n");
                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;
}

SER.C

#include "headsock.h"

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

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

    //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);

    close(sockfd);
    exit(0);
}

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

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

    printf("Start receiving...\n");

    srand(time(NULL)); // seed for random number
    uint8_t prev_pkt_seq = 1;

    while(!end)
    {
        /*************** RECEIVE MESSAGE ***************/
        // if error in receiving
        if ((n = recvfrom(sockfd, &packet, sizeof(packet), 0, (struct sockaddr *)&addr, &len)) == -1)
        {
            printf("Error when receiving\n");
            exit(1);
        }

        // if nothing received
        else if (n == 0)
        {
            printf("Nothing received\n");
        }

        // if something received
        else
        {
            // random number 0-99
            // ACK lost
            // send ACK
            if ((rand() % 100) > NO_ACK_RATE)
            {
                // tell sender what to expect next
                if (packet.num == 0)
                    ack.num = 1;
                else
                    ack.num = 0;
                ack.len = packet.len;

                // random number 0-99
                // ACK damaged
                // damage ACK by toggling ACK
                if ((rand() % 100) < WRONG_ACK_RATE)
                {
                    if (ack.num == 0)
                        ack.num = 1;
                    else
                        ack.num = 0;
                    printf("ACK damaged! ");
                }

                /*************** SEND ACK ***************/
                if ((n = sendto(sockfd, &ack, sizeof(ack), 0, (struct sockaddr *)&addr, len)) == -1)
                {
                    printf("ACK send error!\n");
                    exit(1);
                }
                printf("%i %i as ACK sent\n", ack.num, ack.len);
            }
            // does not send ACK
            else
                printf("ACK lost!\n");

            // only save packet if it is not a duplicate
            if (packet.num != prev_pkt_seq)
            {   
                // if the last bit of the received string is the EoF
                if (packet.data[packet.len-1] == '\0')
                {
                    end = 1;
                    packet.len--;
                }

                // copy this packet
                memcpy((buf+lseek), packet.data, packet.len);
                lseek += packet.len;
            }

            // record down previous packet sequence
            prev_pkt_seq = packet.num;
        }
    }

    if ((fp = fopen ("r.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);
}

header.h

#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <netdb.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <math.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/time.h>
#include <time.h>

#define NEWFILE (O_WRONLY|O_CREAT|O_TRUNC)
#define MYTCP_PORT 4950
#define MYUDP_PORT 5350
#define DATALEN 65
#define BUFSIZE 60000

#define PACKLEN 1000
#define NO_ACK_RATE 0 // 10%
#define WRONG_ACK_RATE 0 // 10%
#define TRUE 1
#define FALSE 0
#define TIMEOUT 1

// data packet structure
struct pack_so
{
    uint32_t num; // the sequence number
    uint32_t len; // the packet length
    char data[PACKLEN]; // the packet data
};

struct ack_so
{
    uint8_t num; // the sequence number
    uint32_t len; // the packet length
};

2 个答案:

答案 0 :(得分:3)

  

据我所知,与TCP不同,UDP有点不可靠。

并不是说'有点'不可靠。它内置了零可靠性功能。因此任何数据包或片段丢失都不会到达。

  

我刚刚完成了无错误版本的实现,并且惊人地发现在我所谓的“无错误”版本中,确实存在ACK丢失的情况!

任何网络中都会丢失数据包。这就是网络如何保护自己免受过载。

  

所以即使在我手动添加错误概率之前,ACK也已经丢失(奇怪的是它永远不会被损坏)。

UDP数据报整个到达或完整到达或根本不到达。没什么好奇的。

  

由于这是一个非常简单的通信,我预计没有ACK丢失或损坏。

这不是一个合理的期望。

  

我想知道我是否在错误的地方实现了它或它应该是这样的?

同样,并不是说“它应该是这样的”:它是没有什么可以阻止它。

重新编写代码,您不能假设任何时间recvfrom()返回-1,这是EWOULDBLOCK条件。你必须检查一下。

答案 1 :(得分:1)

当您使用UDP时,绝对不能保证任何数据都能通过。通过简单地设置两台共享同一网络交换机的计算机,您可以预期数据包到达的几率非常高,但仍然无法保证。

TCP确实有保证......如果数据包丢失,TCP将检测到它并重新发送数据包。

在UDP之上实现自己的可靠性协议将是一件非常痛苦的事。 UDP的通常用例是高冗余数据流,其中低延迟是必不可少的;例如,视频会议应用程序。由于目标是每秒重绘整个屏幕数十次,如果屏幕的一部分在一帧期间无法重绘,则没有可察觉的问题,因此可以通过打开UDP连接并喷洒视频包来完成视频会议。但是每个数据包都需要进行标记,以便在数据包丢失或无序到达时,接收软件可以确定要做什么。

抱歉,我今晚无法看你的节目,但我想告诉你,UDP不仅有点不可靠;它根本没有任何保证。