简体   繁体   English

丢包超时(UDP)

[英]Timeout for dropped packets (UDP)

I'm trying to create a timeout using select() for UDP socket transfer. 我正在尝试使用select()为UDP套接字传输创建超时。 I want to send an int from client to server , wait 300ms, and if I don't get an ACK, resend the packet. 我想将intclient发送到server ,等待300毫秒,如果没有收到ACK,请重新发送数据包。 I'm not sure how to set this up properly with the timeout. 我不确定如何在超时后正确设置此设置。 From what I've gathered online and on the notes I have from class, select should be used on the receiving end. 根据我在网上收集的内容以及课堂上的笔记,应该在接收端使用select

the client at the server send back and forth the numbers 1-100. 服务器上的客户端来回发送数字1-100。 I have a separate router simulated code that randomly drops packets 我有一个单独的router模拟代码,可以随机丢弃数据包

Here is the code i have for the client side 这是我为客户端提供的代码

int sent = 1;
int received = 1;

    for (int i = 0; i < 100; i++)
    {
        string sent1 = to_string(sent);
        char const *pchar = sent1.c_str();
        if(!sendto(s, pchar, sizeof(pchar), 0, (struct  sockaddr*) &sa_in, sizeof(sa_in)))
            cout << "send NOT successful\n";
        else
        {
            cout << "Client sent " << sent << endl;
            sent++;
        }
        // receive
        fd_set readfds; //fd_set is a type
        FD_ZERO(&readfds); //initialize 
        FD_SET(s, &readfds); //put the socket in the set

        if(!(outfds = select (1 , &readfds, NULL, NULL, & timeouts))) 
            break;
        if (outfds == 1) //receive frame
        {
            if (!recvfrom(s, buffer2, sizeof(buffer2), 0, (struct sockaddr*) &client, &client_length))
                cout << "receive NOT successful\n";
            else
            {
                received = atoi(buffer2);
                cout << "Client received " << received << endl;
                received++;
            }
        }
    }

The code is identical for the receiving side except it is in reverse: receive first, then send 接收方的代码是相同的,除了相反:先接收,然后发送

My code doesn't utilize the timeout at all. 我的代码根本没有利用超时。 This is basically what I want to do: 这基本上是我想要做的:

send packet(N)
    if (timeout)
        resend packet(N)
    else
        send packet(N+1)

If the receiver gets a timeout it needs to tell the sender, or else not tell the sender. 如果接收方超时,则需要告知发送方,否则告诉发送方。 In other words you have to implement either a NACK-based protocol or an ACK-based protocol. 换句话说,您必须实现基于NACK的协议或基于ACK的协议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM