Sending a float array from c++ to c# using tcp

Viewed 1534

I am new to tcp, and have set up two applications, one in c++ that sends data, and one in c# that receives it. I send two float arrays, each containing three floats.

The data transfers, and unpacks fine, but the order of the data does not stay consistent. For example, I send float1, float2, float3, and receive float2, float1, float3.

My applicable c++ is:

float position[3];
float rotation[3];

for (int i = 0; i < 3; i++)
        {
            iResult = send(ConnectSocket, (char*)&position[i], (int) sizeof(float),4);
            iResult = send(ConnectSocket, (char*)&rotation[i], (int) sizeof(float),4);
        }
        if (iResult == SOCKET_ERROR) {
            printf("send failed with error: %d\n", WSAGetLastError());
            closesocket(ConnectSocket);
            WSACleanup();
            //return 1;
        }

and c#:

clientSocket = serverSocket.Accept();
Console.WriteLine("Server: Accept() is OK...");
Console.WriteLine("Server: Accepted connection from: {0}", clientSocket.RemoteEndPoint.ToString());

                            // Receive the request from the client in a loop until the client shuts
                            //    the connection down via a Shutdown.
                            Console.WriteLine("Server: Preparing to receive using Receive()...");
                            while (true)
                            {
                                rc = clientSocket.Receive(receiveBuffer);
                                float transX = System.BitConverter.ToSingle(receiveBuffer, 0);
                                float transY = System.BitConverter.ToSingle(receiveBuffer, 4);
                                float transZ = System.BitConverter.ToSingle(receiveBuffer, 8);

                                float rotX = System.BitConverter.ToSingle(receiveBuffer, 12);
                                float rotY = System.BitConverter.ToSingle(receiveBuffer, 16);
                                float rotZ = System.BitConverter.ToSingle(receiveBuffer, 20);


                                Console.WriteLine(transX + " " + transY + " " + transZ + "\n");
                                Console.WriteLine(rotX + " " + rotY + " " + rotZ + "\n");
                               // Console.WriteLine("Server: Read {0} bytes", rc);
                                if (rc == 0)
                                    break;
                            }

What is my error here? Should I be sending the floats individually? Thanks.

2 Answers
Related