[英]Sending a float array from c++ to c# using tcp
我是tcp的新手,已經建立了兩個應用程序,一個在c ++中用於發送數據,另一個在c#中用於接收數據。 我發送兩個浮點數組,每個數組包含三個浮點。
數據可以傳輸和解壓縮,但是數據順序不一致。 例如,我發送float1,float2,float3,並接收float2,float1,float3。
我適用的c ++是:
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;
}
和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;
}
我這是什么錯誤? 我應該單獨發送花車嗎? 謝謝。
第一個錯誤是假設您將完全按照發送數據的方式接收數據。
不幸的是,當發送多個浮點數時,接收器可能會一起接收它們(如您期望的那樣),但是它也可能分成幾部分接收(在假設的最壞情況下,逐字節接收)。
因此,在您的while循環中,例如,您可能僅接收10個字節,但您假設已經接收了24個字節。這會導致大量垃圾。
解決方案:在假定存在數據之前,用rc檢查您是否已接收到正確數量的數據,並以適當管理緩沖區的方式實施循環(即,接收第一組數據的末尾可能與開始時一起出現)下一個序列)。
還有另一個更微妙的問題 :您的代碼邏輯僅在源和目標使用相同的浮點編碼時才有效。 C ++標准不能解決此編碼問題(通常是IEEE-754,但不一定如此,即使是這樣,不同的字節序也可能會影響通過網絡發送的字節順序 。
如果您僅針對Wintel平台,這可能不是問題。 但是我認為值得一提的是,如果您的C ++部分是針對使用不同架構的IoT設備的;-)
編輯:處理緩沖接收的提示
我不精通C#,但是原理如下:
Console.WriteLine("Server: Preparing to receive using Receive()...");
rc=1;
br=0; // number of bytes in buffer
sf=24; // size of one sequence to be received
while (true)
{
while (br<sf && rc>0) { // fill buffer until all bytes of one sequence are there
rc = clientSocket.Receive(receiveBuffer, br, sf-br,SocketFlags.None);
br += rc;
}
if (rc == 0) {
if (br>0)
Console.WriteLine("Server: interupted while receiving data...");
break;
}
//... here you can convert the content of the buffer
}
您發送花車的順序如下:
pos1
rot1
pos2
rot2
pos3
rot3
但是,在接收它們時,您將像這樣處理數據:
pos1
pos2
pos3
rot1
rot2
rot3
解決方案是從正確的索引中讀取浮點數。
float transX = System.BitConverter.ToSingle(receiveBuffer, 0);
float transY = System.BitConverter.ToSingle(receiveBuffer, 8);
float transZ = System.BitConverter.ToSingle(receiveBuffer, 16);
float rotX = System.BitConverter.ToSingle(receiveBuffer, 4);
float rotY = System.BitConverter.ToSingle(receiveBuffer, 12);
float rotZ = System.BitConverter.ToSingle(receiveBuffer, 20);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.