I have a UDP message sending socket in my Windows 10 laptop, I am trying to send a JSON to be decoded on ROS. I wrote down a basic user entry function from command line, if I enter 1 it will send JSON with hello, if I enter 2 it will send jasonn with hi. The problem is it sends bot hi AND hello message packages when I enter 1. I do not know the reason , whether wrong logic on switch statement or something wrong with sendto() function of winsock2.
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;
}
BufLen,// this will be json buffers length
But it's not!
You send BufLen
bytes regardless of which message is being sent.
It's likely that this is too large and you're accidentally overrunning SendBufHI
into a string literal that happens to live next to it in memory.
Send only the bytes that you want to. That could mean strlen(SendBufHI)
.