2017-12-14 7 views
0

私は慣性キューブトラッカーを使用して、ヨーピッチとロール(積み重ね)その情報をネットワークに接続するためにその情報を読み取るサーバーを設定する必要があります。これまでのところ、クライアントとサーバーを作成しましたが、問題は1つのチャンク内に情報を送信し、それを3つ分読み取って出力するか、またはどの受信と一致するかを指定することです。Winsock 2、変数を文字列に凝縮して送信し、それを受信して​​から読み取る

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; 
       } 
      } 
     } 

これは私のクライアント側であり、ここでは私のサーバー側を入れます。ネットワークは接続し、私のトラッカーコードはうまく動作しますが、送信と受信はすべて勝ちです。

//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); 

私のC++に関する知識は素晴らしいものではなく、どんな助けも素敵でしょう。ありがとう!

答えて

1

コードにはさまざまな種類があります。誤解を解消して修正しようとします(私はあなたがTCPを使っていると仮定します)。あるサイズのバッファを送信していますが、別のサイズのバッファを潜在的にrecvしています。 floatであるsizeof(yaw)は、このfloatの文字列表現のサイズと同じではありません。

個々のアイテムの発信/受信は遅いです。理想的には、簡単なプロトコルを定義します。このプロトコルのメッセージは、送信するすべての値を含む文字列になります。単一のメッセージを使用してそのメッセージを送信するsend()受信側では、データストリームを読み込み、完全なメッセージを受信したときに通知する特定のマーカーを探します。次に、そのメッセージを処理して、さまざまなコンポーネントをヨー/ピッチ/ロール変数に分割します。

文字列メッセージの例

は次のようになります。あなたは「}」その後、あなたがこのメッセージを処理し、さまざまなコンポーネントを解析することができます達するまで

"{yaw=1.34;pitch=2.45;roll=5.67}"はその後、クライアント上で、あなたは継続的にバッファにデータを読み込みます。

関連する問題