c++ - recvfrom 超时(winsock2 和 udp)

标签 c++ c udp winsock2

我正在尝试在 recvfrom() 函数上实现超时

为此,我使用 select() 函数。我从伟大的互联网上获取了代码,但不知道为什么,当我使用它时,我的程序崩溃了。 如果有帮助的话,我会在线程中启动此服务器。 这是我尝试做的:

...
        // Setup timeval variable
        timeval timeout;
        timeout.tv_sec = 5;
        timeout.tv_usec = 0;

        // Setup fd_set structure
        fd_set fds;
        FD_ZERO(&fds);
        FD_SET(id_de_la_socket, &fds);

        // Return value:
        // -1: error occurred
        // 0: timed out
        // > 0: data ready to be read
        int retval = select(id_de_la_socket+1, &fds, NULL, NULL, &timeout);
        if(retval == -1){
            printf("Error");
            return NULL;
        }
        else if(retval == 0){
            printf("Timeout");
            return NULL;
        }
        else{
            nombre_de_caractere=recvfrom(id_de_la_socket,buffer,1515,/*MSG_PARTIAL*/0,(struct sockaddr*)&information_sur_la_source,&tempo);
...

在尝试实现超时之前,一切正常,我的recvfrom()处于 block 模式。所以我认为问题出在我添加的这段代码上。也许是我不太理解的函数参数。

感谢您的帮助。

编辑:完整代码:

#include "serveur.h"
#include <unistd.h>
#include <fcntl.h>
serveur::serveur()
{
}

StructureSupervision::T_StructureSupervision* serveur::receiveDataUDP(){
    WSADATA initialisation_win32;
    int erreur; 
    int tempo; 
    int nombre_de_caractere; 
    char buffer[65535]; 
    SOCKET id_de_la_socket; 
    SOCKADDR_IN information_sur_la_source; 

    erreur=WSAStartup(MAKEWORD(2,2),&initialisation_win32);
    if (erreur!=0)
        printf("\nDesole, je ne peux pas initialiser Winsock du a l'erreur : %d %d",erreur,WSAGetLastError());
    else
        printf("\nWSAStartup  : OK");


    id_de_la_socket=socket(AF_INET,SOCK_DGRAM,0);
    if (id_de_la_socket==INVALID_SOCKET)
        printf("\nDesole, je ne peux pas creer la socket du a l'erreur : %d",WSAGetLastError());
    else
        printf("\nsocket      : OK");


    information_sur_la_source.sin_family=AF_INET;
    information_sur_la_source.sin_addr.s_addr = inet_addr("10.100.13.129"); // Ecoute sur toutes les IP locales
    information_sur_la_source.sin_port=htons(4000); // Ecoute sur le port 4000
    erreur=bind(id_de_la_socket,(struct sockaddr*)&information_sur_la_source,sizeof(information_sur_la_source));
    if (erreur!=0)
        printf("\nDesole, je ne peux pas ecouter ce port : %d %d",erreur,WSAGetLastError());
    else
        printf("\nbind        : OK \n");


    tempo=sizeof(information_sur_la_source); // Passe par une variable afin d'utiliser un pointeur

    // Setup timeval variable
    timeval timeout;
    timeout.tv_sec = 5;
    timeout.tv_usec = 0;
    // Setup fd_set structure
    fd_set fds;
    FD_ZERO(&fds);
    FD_SET(id_de_la_socket, &fds);
    // Return value:
    // -1: error occurred
    // 0: timed out
    // > 0: data ready to be read
    ULONG NonBlock = 1; ioctlsocket(id_de_la_socket, FIONBIO, &NonBlock);
    int retval = select(id_de_la_socket+1, &fds, NULL, NULL, &timeout);
    if(retval == -1){
        printf("Error");
        return NULL;
    }
    else if(retval == 0){
        printf("Timeout");
        return NULL;
    }
    else{
    nombre_de_caractere=recvfrom(id_de_la_socket,buffer,1515,/*MSG_PARTIAL*/0,(struct sockaddr*)&information_sur_la_source,&tempo);
    buffer[nombre_de_caractere]=0; // Permet de fermer le tableau apr�s le contenu des data, car la fonction recvfrom ne le fait pas
    //printf("\nVoici les donnees : %s",buffer);
    StructureSupervision::T_StructureSupervision *structureReception = (StructureSupervision::T_StructureSupervision *) buffer;
    std::cout << "Voici le numero de Statut Ground Flight : " << structureReception->SystemData._statutGroundFlight;


    erreur=closesocket(id_de_la_socket);
    if (erreur!=0)
        printf("\nDesole, je ne peux pas liberer la socket du a l'erreur : %d %d",erreur,WSAGetLastError());
    else
        printf("\nclosesocket : OK");

    // ********************************************************
    // Quitte proprement le winsock ouvert avec la commande WSAStartup
    // ********************************************************
    erreur=WSACleanup(); // A appeler autant de fois qu'il a �t� ouvert.
    if (erreur!=0)
        printf("\nDesole, je ne peux pas liberer winsock du a l'erreur : %d %d",erreur,WSAGetLastError());
    else
        printf("\nWSACleanup  : OK");

    return structureReception;
    }
}

最佳答案

感谢您的帮助,问题是:该程序在循环中运行,因此我在错误的时间打开和关闭套接字。

关于c++ - recvfrom 超时(winsock2 和 udp),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24506975/

相关文章:

计算动态数组读取的复杂度

command-line - 帮助 : UDP broadcast vlc stream weirdness!

c++ - 为 UDP 服务器响应重用 sock_fd 与新 sock_fd

c - 使用 C 绑定(bind)到 IOCP 的 UDP 套接字

c - 顺序逐字节比较

c - 在 C 中用数组绘制轨迹图

c++ - 处理巨大的文本文件

c++ - 类似于 glib 的对象模型?

c++ - static_cast<char*> 和 (char*) 之间的区别

c++ - 重定向 boost 绑定(bind)