Preston Schlagel
Preston Schlagel

Reputation: 1

Winsock 2, condensing variables into a string, sending it out, then recieving it and reading it back

I am writing some code that involves using an inertia cube tracker, that actively changes its yaw pitch and roll (in degrees) and I need to set up a server that reads that information in order to network the info. So far I have created a client and server, but the problem I am having is either to send the information in one chunck then read it back as three and print it, or to specify which send matches with which recieve.

if( currentTrackerH > 0 )
            {
                int iSendResult1;
                int iSendResult2;
                int iSendResult3;

                char EulerBuffer0[64];
                char EulerBuffer1[64];
                char EulerBuffer2[64];


                showStationData( currentTrackerH, &TrackerInfo,
                                 &Stations[station-1], &data.Station[station-1],
                                 &StationsHwInfo[currentTrackerH-1][station-1],
                                 showTemp);
                //send to the server 
                do{
                sprintf(EulerBuffer0, "%f", data.Station[station-1].Euler[0]);
                iSendResult1= send(Connection, EulerBuffer0, sizeof(data.Station[station-1].Euler[0]), NULL);

                sprintf(EulerBuffer1, "%f", data.Station[station-1].Euler[1]);
                iSendResult2= send(Connection, EulerBuffer1, sizeof(data.Station[station-1].Euler[1]), NULL);

                sprintf(EulerBuffer2, "%f", data.Station[station-1].Euler[2]);
                iSendResult3= send(Connection, EulerBuffer2, sizeof(data.Station[station-1].Euler[2]), NULL);
                }while ((iSendResult1 || iSendResult2 || iSendResult3)>0);
                //shutdown the socket when there is no more data to send    
                iSendResult1 = shutdown(Connection, SD_SEND);
                if (iSendResult1 == SOCKET_ERROR) 
                {
                    printf("shutdown failed with error: %d\n", WSAGetLastError());
                    closesocket(Connection);
                    WSACleanup();
                    return 1;
                }
            }
        }

This is my client side and here I will put my server side. The networks connect and my tracker code works just fine but sending and recieving is where it all gets wonky.

//begin recieving data
char yaw[256];
char pitch[256];
char roll[256];

int iResult1;
int iResult2;
int iResult3;

float fyaw, fpitch, froll;

do{
    do {    
    iResult1= recv(newConnection, yaw,sizeof(yaw),NULL);
    } while( iResult1 == 0 );

    fyaw = atof(yaw);

    do {    
    iResult2= recv(newConnection, pitch,sizeof(pitch),NULL);
    } while( iResult1 == 0 );

    fpitch = atof(pitch);

    do {    
    iResult3= recv(newConnection, roll,sizeof(roll),NULL);
    } while( iResult1 == 0 );

    froll = atof(roll);

    printf("(%f,%f,%f)deg \n",
                fyaw, fpitch, froll);
}while(1);

my knowledge of c++ is not fantastic and any help would be lovely. Thanks!

Upvotes: 0

Views: 27

Answers (1)

Eyal Cinamon
Eyal Cinamon

Reputation: 959

There is all kinds of wrong in your code. Let's try to break down and correct misconceptions (I assume you're using TCP.) You are sending buffers of one size, but recv'ing potentially a buffer of another size. sizeof(yaw) which is a float, is not the same as the size of the string representation of this float.

Calling send/recv for individual item is slow. Ideally you would define a simple protocol. A message in this protocol would be a string containing all the values you wish to transmit. You send that message using a single send() On the receiving side you read in the stream of data, and look for specific markers that tell you when you have received a complete message. You then process that message, splitting out the different components into your yaw/pitch/roll variables.

An example of a string message would be: "{yaw=1.34;pitch=2.45;roll=5.67}"

Then on the client you continually read into a buffer your data until you reach the "}" Then you can process this message and parse out the different components.

Upvotes: 1

Related Questions