switch语句导致发送两条消息

时间:2019-07-17 16:07:30

标签: c++ udp ros winsock2

我的Windows 10笔记本电脑中有一个UDP消息发送套接字,我正在尝试发送要在ROS上解码的JSON。 我从命令行写下了一个基本的用户输入函数,如果输入1,它将发送带有hello的JSON,如果输入2,则将发送带有hi的jasonn。 问题是当我输入1时它会发送bot hi和hello消息包。 我不知道原因,无论是switch语句逻辑错误还是winsock2的sendto()函数出错。

userDialog::UserEntry getUserEntrySendMessage(SOCKET myUdpSocket, sockaddr_in rosBridgeUdpAddress, int addrLen, int BufLen)
{
    std::cout<<"please enter what do you like to do:\n";
    std::cout << "enter 0 to connect ROSpberry Pi \n";
    std::cout << "enter 1 to send HI to ROSpberry Pi\n";
    std::cout << "enter 2 to send HELLO to ROSpberry Pi\n";
    std::cout << "enter 3 to finish \n";
    std::cout << "enter one of those in all UPPERCASE, "
        << "this code does not have an error check yet!\n";
    userDialog::UserEntry userInput{};
    int x;
    std::cin >> x;
    userInput = static_cast<userDialog::UserEntry>(x);
    switch (userInput)
    {
    case userDialog::CONNECT_ROS:
        {
        //creating the JSON message to be sent for initiating the topic
            const char* SendBufSTART = { "{ \"op\": \"advertise\",  \"topic\": \"myTopic\",  \"type\": \"std_msgs/String\" \}" };
            std::cout << "I will send this : \n" << SendBufSTART << "\n"; //quick check if JSON is correctly constructed
            sendto(
                myUdpSocket,
                SendBufSTART, //this will be our json 
                BufLen,// this will be json buffers length
                0, //no flags
                (SOCKADDR*)& rosBridgeUdpAddress,
                //sizeof(rosBridgeUdpAddress)   );
                addrLen);
            return userDialog::CONNECT_ROS;
        }
        break;
    case userDialog::SAY_HI:
    {
        //creating the JSON message to be sent to be published inside the topic
        const char* SendBufHI = { "{ \"op\": \"publish\", \"id\": \"metin's laptop\",  \"topic\": \"myTopic\",  \"msg\": \{\"data\": \"HI from metin!\"\} \}" };
        std::cout << "I will send this : \n" << SendBufHI << "\n"; //quick check if JSON is correctly constructed
        sendto(
            myUdpSocket,
            SendBufHI, //this will be our json 
            BufLen,// this will be json buffers length
            0, //no flags
            (SOCKADDR*)& rosBridgeUdpAddress,
            //sizeof(rosBridgeUdpAddress)   );
            addrLen);
        return userDialog::SAY_HI;
    }
        break;
    case userDialog::SAY_HELLO:
    {
        //creating the JSON message to be sent to be published inside the topic
        const char* SendBufHELLO = { "{ \"op\": \"publish\", \"id\": \"metin's laptop\",  \"topic\": \"myTopic\",  \"msg\": \{\"data\": \"HELLO from metin!\"\} \}" };
        std::cout << "I will send this : \n" << SendBufHELLO << "\n"; //quick check if JSON is correctly constructed
        sendto(
            myUdpSocket,
            SendBufHELLO, //this will be our json 
            BufLen,// this will be json buffers length
            0, //no flags
            (SOCKADDR*)& rosBridgeUdpAddress,
            //sizeof(rosBridgeUdpAddress)   );
            addrLen);
        return userDialog::SAY_HELLO;
    }
        break;
    default:
        return userDialog::DIALOG_FINAL;
    }
    return userDialog::DIALOG_FINAL;
}

1 个答案:

答案 0 :(得分:4)

BufLen,// this will be json buffers length

但是不是!

无论发送哪个消息,您都发送BufLen字节。

这可能是太大,并且您不小心将SendBufHI改写为字符串文字,而该文字恰好位于内存中。

仅发送您想要的字节。这可能意味着strlen(SendBufHI)(尽管对于单数据包网络传输,我可能会跳过结尾处带有- 1的空终止符)。