Note that there are some explanatory texts on larger screens.

plurals
  1. POConverting uint32_t to network byte order
    text
    copied!<p>i'm doing stuff for my studies, so yeah, it's <em>homework</em>.</p> <p>First: I got a working Java Server Application, i also had this client application i'm writing in c now working in java. Simple as is, i fail at sending the uint32_t to my java server.</p> <p>So let's show off some code:</p> <pre><code>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) &lt; 0) { printf("write: Konnte keine Daten zum gegenüber senden.\n"); exit(EXIT_FAILURE); } char* retVal = "hallo"; return retVal; } </code></pre> <p>Simple as is, i call this function with func = 'v', str = "hallo" and anz = 2 which will give me a packageLength of 27 bytes.</p> <p>Package is build like this:<br /> = packageLength (int) which is 4 byte<br /> + length of function descriptor (funcLength) which is 4 byte<br /> + func descriptor (func) which is funcLength<br /> + length of param1 (strLength) which is 4 byte<br /> + length of value of param1 (str) which is strLength<br /> + length of param2 which is 4 byte<br /> + length of value of param2 (anz) which is 4 byte<br /> + NULL Byte (\0) which is 1 byte<br /></p> <p>I assume the conversion i do is wrong maybe i'm using the wrong datatype. On server-side i use the Java ByteBuffer to collect the data. At first i read the 4 byte packagelength from the network which will give me the information of how long i have to read until i get the whole data package:</p> <pre><code>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); </code></pre> <p>The last println will give me the following output:</p> <blockquote> <p>packageLength875901497</p> </blockquote> <p>So does anyone now where my problem is? I can provide you with more code if necessary but i think the error is either the datatype (uint32_t) or the conversion.</p> <p>Help is appriciated. Thanks in advance.</p> <p>Cheers Daniel</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload