0
votes

I am trying to let my c++ program communicate with simulink model via udp, and in particularly the plan is that data will be transmitted from c++ to simulink, and will be processed using some simulink models, then the result will be sent back to c++ for further programming, and data trasmissions will be realized through udp, (udp socket for c++, and udp real time send/receive block for simulink). And it will repeat a few times.

I've built a while loop, where c++ keeps reading data from a txt file and sends it to simulink via udp, which works well, the data has been multiplied by 2 and sent back towards c++, but c++ cannot receive correct data as was sent from simulink. The data it received during each cycle is "0". But if the data I sent was a constant, the receiving function in c++ works fine as well.


ifstream file_x("x1.txt");
ifstream file_y("y1.txt");
ifstream file_z("z1.txt");

double x;
double y;
double z;

int main() 
{ 
    //Local Varaiable definition
    cout<<"\t\t--------------------UDP Server----------------"<<endl;
    cout<<endl;

    WSADATA WinsockData;
    int     iWsaStartup;
    int     iWsaCleanup;

    SOCKET  UDPSocketServer1;
    SOCKET  UDPSocketClient3;
    struct  sockaddr_in UDPClient1;
    struct  sockaddr_in UDPServer3;

    char    Buffer1[200];
    char    Buffer3[200];

    int     iBufferLen1 = 200+1;
    int     iBufferLen3 = 200+1;

    int         iBind3;
    int     iReceiveFrom;
    int     iSendTo;

    int     iUDPClientLen1  = sizeof(UDPClient1);
    int     iUDPClientLen2  = sizeof(UDPClient2);
    int     iUDPServerLen3  = sizeof(UDPServer3);
    int     iCloseSocket1;
    int     iCloseSocket3;

    //STEP-1: initialization of Winsock
    iWsaStartup = WSAStartup(MAKEWORD(2,2), &WinsockData);
    if (iWsaStartup != 0)
    {
        cout<<"WSAStartUp Fun Failed!"<<endl;
    }
    else
    {
        cout<<"WSAStartUp Success"<<endl;
    }

    //STEP-2: Fill the UDPClient(SOCKET ADDRESS) Structure
    UDPClient1.sin_family      = AF_INET;
    UDPClient1.sin_addr.s_addr = inet_addr("127.0.0.1");
    UDPClient1.sin_port        = htons(8001);

    UDPServer3.sin_family      = AF_INET;
    UDPServer3.sin_addr.s_addr = inet_addr("127.0.0.3");
    UDPServer3.sin_port        = htons(8003);

    //STEP-3: Socket Creation
    UDPSocketServer1 = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (UDPSocketServer1 == INVALID_SOCKET)
    {
        cout<<"Socket1 Creation Failed & Error No ->"<<WSAGetLastError()<<endl;
    }
    else
    {
        cout<<"Socket1 Creation Success"<<endl;
    }

    UDPSocketClient3 = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (UDPSocketClient3 == INVALID_SOCKET)
    {
        cout<<"Socket3 reation Failed = "<<WSAGetLastError()<<endl;
    }
    else
    {
        cout<<"Socket3 Creation Success"<<endl;
    }

    //STEP-4: Bind the server
    iBind3 = bind(UDPSocketClient3, (SOCKADDR*)&UDPServer3, sizeof(UDPServer3));
    if (iBind3 == SOCKET_ERROR)
    {
        cout<<"Binding Failed & Error No ->"<<WSAGetLastError()<<endl;
    }

    //STEP-4-1: SendTo Fun send data to client  
    while (!file_x.eof())
    {
        file_x     >> x;
        file_y     >> y;
        file_z     >> z;

        sprintf_s(Buffer1,200,"%0.2f\r\n %0.2f\r\n %0.2f\r\n",x,y,z);

        iSendTo = sendto(UDPSocketServer1, Buffer1, iBufferLen1, MSG_DONTROUTE, (SOCKADDR*)&UDPClient1, sizeof(UDPClient1));
        if (iSendTo == SOCKET_ERROR)
        {
            cout<<"Sending Data Failed & Error No ->"<<WSAGetLastError()<<endl;
        }
        else
        {
            cout<<"Sending Data Success"<<endl;
        }

        iReceiveFrom = recvfrom(UDPSocketClient3, Buffer3, iBufferLen3, MSG_PEEK, (SOCKADDR*)&UDPServer3, &iUDPServerLen3);
        if (iReceiveFrom == SOCKET_ERROR)
        {
            cout<<"Receiving failed & Error No ->"<<WSAGetLastError()<<endl;
        }
        else
        {
            cout<<"Receiving Success"<<endl;
            cout<<"Receive Data ->"<<Buffer3<<endl;
        }       

    }


    //STEP-6: CloseSocket Function
    iCloseSocket1 = closesocket(UDPSocketServer1);
    if (iCloseSocket1 == SOCKET_ERROR)
    {
        cout<<"Socket1 Closing failed & Error No ->"<<WSAGetLastError()<<endl;
    }
    else
    {
        cout<<"Socket1 Closing Success"<<endl;
    }

    iCloseSocket3 = closesocket(UDPSocketClient3);
    if (iCloseSocket3 == SOCKET_ERROR)
    {
        cout<<"Socket3 Closing failed & Error No ->"<<WSAGetLastError()<<endl;
    }
    else
    {
        cout<<"Socket3 Closing Success"<<endl;
    }
    //STEP-7: WSACleanUp Fun for Terminating from DLL
    iWsaCleanup = WSACleanup();
    if (iWsaCleanup == SOCKET_ERROR)
    {
        cout<<"WSA Cleanup Success"<<endl;
    }
    else
    {
        cout<<"WSA Cleanup Success"<<endl;
    }

    system("pause");
    return 0;
} 

1

1 Answers

0
votes

I found that flag in recvfrom function matters in this issue, which can be referred to its document and I've changed MSG_PEEK to 0 and considering my application, I've used another project for only receiving data sent from simulink, and the issue has been solved.