Convertire UINT32_T in Ordine di byte di rete
-
12-11-2019 - |
Domanda
Sto facendo cose per i miei studi, quindi sì, lo è compiti a casa.
Innanzitutto: ho ottenuto un'applicazione Java Server funzionante, avevo anche questa applicazione client che sto scrivendo in C ora lavorando in Java. Semplice come è, non riesco a inviare l'UINT32_T al mio server Java.
Quindi mettiamo in mostra un po 'di codice:
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;
}
Semplice come è, chiamo questa funzione con func = 'v', str = "hallo" e anz = 2 che mi darà una lunghezza packagele di 27 byte.
Il pacchetto è costruito in questo modo:
= packagelength (int) che è 4 byte
+ Lunghezza del descrittore delle funzioni (funcLength) che è 4 byte
+ Func Descriptor (Func) che è funcLength
+ lunghezza di param1 (stronghenth) che è a 4 byte
+ Lunghezza del valore di Param1 (STR) che è stronzata
+ lunghezza di param2 che è a 4 byte
+ lunghezza del valore di param2 (anz) che è 4 byte
+ Byte null ( 0) che è 1 byte
Presumo che la conversione che faccio sia sbagliata forse sto usando il tipo di dati sbagliato. Sul lato server utilizzo il bytebuffer Java per raccogliere i dati. Inizialmente ho letto la lunghezza di packagelele a 4 byte dalla rete che mi darà le informazioni su quanto tempo devo leggere fino a quando non ottengo l'intero pacchetto di dati:
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);
L'ultimo println mi darà il seguente output:
packagelength875901497
Quindi qualcuno ora dove è il mio problema? Posso fornirti più codice, se necessario, ma penso che l'errore sia il tipo di dati (uint32_t) o la conversione.
L'aiuto è apprenduto. Grazie in anticipo.
Saluti Daniel
Nessuna soluzione corretta