C ++ WinSock:通过UDP发送大整数值(long long)

时间:2016-12-05 19:36:24

标签: c++ udp timestamp winsock long-integer

我试图在通过winsock发送的UDP数据包中包含时间戳。为此,我将std :: chrono :: time_point的持续时间作为long long发送。这是正在发送的结构:

struct PlayMessage
{ // Signals that both players are connected and should begin the game
    MessageType type;

    // Time used to synchronised applications ideas of time
    long long syncTime;

    PlayMessage()
        : type(MT_PLAY)
    {
    }
};

这就是我创建消息的方式:

long long sync = the_clock::now().time_since_epoch().count();

    while (message1 == false)
    {
        // Create play message
        PlayMessage play;
        play.type = MT_PLAY;
        play.syncTime = sync; // Set current clock time as authoritative time accross applications

        // Send replies
        int sendcount = sendto(sock, (const char *)&play, sizeof(PlayMessage),
            0, (const sockaddr *)&player1Addr, sizeof(player1Addr));
        if (sendcount == SOCKET_ERROR)
        {
            die("Sending play to client returned error");
        }
        else if (sendcount != sizeof(PlayMessage))
        {
            die("Whole message was not sent");
        }
        else
        {
            // Sent
            message1 = true;
        }
    }

此时,在sendto函数中设置断点时,消息包含时间戳的正确值:

syncTime    14809661934978444   __int64

编辑:哎呀,这是接收代码客户端!

// Wait for one of the sockets to become readable
        int selcount = select(0, &readable, NULL, NULL, &timeout);
        if (selcount == SOCKET_ERROR)
        {
            Break("select failed");
        }
        if (FD_ISSET(sock, &readable))
        {
            int reccount = recv(sock, playBuffer, sizeof(PlayMessage), 0);
            if (reccount < 0)
            {
                int error = WSAGetLastError();
                Break("recvfrom failed");
            }
            if (reccount != sizeof(PlayMessage))
            {
                Break("received odd-sized message");
            }
        }

        PlayMessage recvd;
        memcpy(&recvd, playBuffer, sizeof(PlayMessage));

        // Check message type
        if (recvd.type != MT_PLAY)
        {
            // Not a player request, continue with loop
            continue;
        }
        else
        {
            // Play
            play = true;

            // Store data into new struct
            PlayMessage recvd;
            memcpy(&recvd, echoBuffer, sizeof(PlayMessage));

            // Store server-authoritative time 
            long long duration = recvd.syncTime + duration_cast<milliseconds>(latency / 2).count();
            start = the_clock::time_point(milliseconds(duration));

            // Now all three applications have a *similar* idea of the current time
        }

在客户端,读取消息时,所有内容都归零(包括消息类型。)

-       recvd   {type=MT_UNKNOWN (0) syncTime=0 }   PlayMessage
                type      MT_UNKNOWN (0)    MessageType
                syncTime       0            __int64

这可能是因为发送的整数的大小?我担心发送时数据包可能太大,或者在完全读取之前,select函数可能在客户端超时?任何帮助将不胜感激,谢谢!

0 个答案:

没有答案