私は勉強のために何かをしているので、ええ、それは宿題です。
最初に:動作するJavaサーバーアプリケーションを入手しました。また、cで書いているこのクライアントアプリケーションもJavaで動作しています。単純なままですが、uint32_tをJavaサーバーに送信できません。
それでは、いくつかのコードを披露しましょう。
char* callString(char func, char* str, uint32_t anz) {
uint32_t packageLength, funcLength, strLength;
funcLength = htonl(sizeof(func));
strLength = htonl(strlen(str));
uint32_t byte = htonl(4);
uint32_t trailing = htonl(1);
anz = htonl(anz);
packageLength = byte + byte + funcLength + byte + strLength + byte + byte + trailing;
/*packageLength = htonl(packageLength);*/
char byteBuffer[ntohl(packageLength)];
printf("%i\n", packageLength);
printf("%i\n", htonl(packageLength));
printf("%i\n", ntohl(packageLength));
sprintf(byteBuffer, "%i%i%c%i%s%i%i%c", packageLength, byte, func, byte, str, byte, anz, '\0');
printf("%s\n", byteBuffer);
if(write(sock, byteBuffer, packageLength) < 0) {
printf("write: Konnte keine Daten zum gegenüber senden.\n");
exit(EXIT_FAILURE);
}
char* retVal = "hallo";
return retVal;
}
そのまま簡単に、この関数をfunc ='v'、str = "hallo"、anz = 2で呼び出します。これにより、27バイトのpackageLengthが得られます。
パッケージは次のようにビルドされます。
=packageLength(int)は4バイト
+関数記述子の長さ(funcLength)は4バイト
+ func記述子(func)はfuncLength
+ param1の長さ(strLength)は4バイト
+長さstrLengthであるparam1(str)の値+4
バイトであるparam2の長さ+1バイト
であるparam2(anz)の値の長さ
+ 1バイトであるNULLバイト(\ 0)
私が行った変換が間違っていると思います。おそらく間違ったデータ型を使用しています。サーバー側では、JavaByteBufferを使用してデータを収集します。最初に、ネットワークから4バイトのパッケージ長を読み取ります。これにより、データパッケージ全体を取得するまでに読み取る必要がある時間の情報が得られます。
byte[] msgLength = new byte[4];
try {
handle.getInputStream().read(msgLength);
} catch (IOException ex) {
System.out.println("could not receive byte stream: msgLength");
break;
}
ByteBuffer receive;
receive = ByteBuffer.wrap(msgLength);
int packageLength = receive.getInt();
System.out.println("packageLength" + packageLength);
最後のprintlnは、次の出力を提供します。
packageLength875901497
それで、私の問題がどこにあるのか、誰かいますか?必要に応じてさらにコードを提供できますが、エラーはデータ型(uint32_t)または変換のいずれかであると思います。
ヘルプが適用されます。前もって感謝します。
乾杯ダニエル