c++ - UDP winsock 服务器 C++

标签 c++ sockets network-programming udp winsock

我正在使用 UDP 连接进行一些套接字编程。我已经编写了我的服务器(它也需要一个 NTP 服务器的偏移量),但是当我对其进行单元测试时,它在 recvfrom 函数中失败了。我是 winsock 编程和一般网络编程的新手,所以我不确定从这里到哪里去。这是我的代码:

NtpServer::NtpServer(u_short portnum, const std::chrono::nanoseconds desiredOffset) : portnum(0), client_length(0), bytes_received(0), current_time(0), desiredOffset(0)
{
    WORD wVersionRequested;
    wVersionRequested = MAKEWORD(2, 2);
    WSADATA wsaData;

    int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (WSAStartup(wVersionRequested, &wsaData) != 0)
    {

        std::cerr << "Could not open Windows connection" << std::endl;
        exit(0);
    }

    sd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sd == INVALID_SOCKET)
    {
        std::cerr << "Could not create socket." << std::endl;
        WSACleanup();
        exit(0);
    }

    //blocking enabled
    u_long iMode = 0;

    iResult = ioctlsocket(sd, FIONBIO, &iMode);
    if (iResult != NO_ERROR)
        std::cerr << "ioctlsocket failed with error: " << iResult << std::endl;


    memset((void *)&server, '\0', sizeof(struct sockaddr_in));
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = htonl(INADDR_ANY);
    server.sin_port = htons(portnum);


    if (bind(sd, reinterpret_cast<SOCKADDR *>(&server),
        sizeof(server)) == -1)
    {
        std::cerr << "Could not bind name to socket" << std::endl;
        closesocket(sd);
        WSACleanup();
        exit(0);
    }
    getResult(desiredOffset);
}

和 getResult 函数:

void NtpServer::getResult(const std::chrono::nanoseconds desiredOffset)
{
    ntp_data ntpData = ntp_data();
    //struct for timeout
    struct timeval tv;
    tv.tv_sec = 10;  // 10 Secs Timeout 
    setsockopt(sd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval));

    while (1)
    {
        client_length = (int)sizeof(struct sockaddr_in);

        /* Receive bytes from client */
        bytes_received = recvfrom(sd, sendBuffer, NTP_PACKET_MAX, 0, (struct sockaddr *)&client, &client_length);
        if (bytes_received < NTP_PACKET_MIN)
        {
            std::cerr << "Could not receive datagram." << std::endl;
            closesocket(sd);
            WSACleanup();
            exit(0);
        }



        /* Check for time request */
        if (strcmp(readBuffer, "GET TIME\r\n") == 0)
        {
            /* Get current time */
            system_clock::time_point now = std::chrono::system_clock::now();
            auto timepointoffset = (now + desiredOffset).time_since_epoch();
            double current_value = std::chrono::duration_cast<std::chrono::duration<double>>(timepointoffset).count();

            unpack_ntp(&ntpData, (unsigned char*)readBuffer, bytes_received);
            make_packet(&ntpData, NTP_CLIENT, current_value);
            pack_ntp((unsigned char *)sendBuffer, NTP_PACKET_MIN, &ntpData);


            /* Send data back */
            if (sendto(sd, sendBuffer,
                (int)sizeof(sendBuffer), 0,
                (struct sockaddr *)&client, client_length) !=
                (int)sizeof(current_time))
            {
                std::cerr << "Error sending datagram." << std::endl;
                closesocket(sd);
                WSACleanup();
                exit(0);
            }
        }
    }
        closesocket(sd);
        WSACleanup();


}

最佳答案

您必须测试来自 recvfrom() 的错误——您正在设置 SO_RCVTIMEO,因此您可以预期接收超时。

真的想要启用读取超时吗?如果是这样,当您设置 SO_RCVTIMEO 时,设置 tv.tv_usec = 0。你离开它未初始化。如果 tv_usec 恰好是一个无效值(小于 0 或大于 999,999),则 setsockopt() 调用应该失败。

关于c++ - UDP winsock 服务器 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33878856/

相关文章:

java - 实现网络协议(protocol) - 关于设计和性能的思考

sockets - 是否有一个程序可以通过套接字将数据发送到服务器以进行测试?

iphone - Wifi设备到设备通信问题

c - 在 c 中调用 select 后超时是否改变?

javascript - Javascript 和 Python 之间的 Web Socket

c++ - Clock_gettime() 函数输出不正确的时间

c++ - DLL中静态变量的状态

Python - 如何检查套接字是否仍然连接

c++ - 删除对象时指向自动为空的指针

c++ - JSON 或 Protocol Buffer 替换自定义 TCP 消息