Converting uint32_t to network byte order

3k views Asked by At

i'm doing stuff for my studies, so yeah, it's homework.

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.

So let's show off some 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) < 0) {
    printf("write: Konnte keine Daten zum gegenüber senden.\n");
    exit(EXIT_FAILURE);
  }
  char* retVal = "hallo";
  return retVal;
}

Simple as is, i call this function with func = 'v', str = "hallo" and anz = 2 which will give me a packageLength of 27 bytes.

Package is build like this:
= packageLength (int) which is 4 byte
+ length of function descriptor (funcLength) which is 4 byte
+ func descriptor (func) which is funcLength
+ length of param1 (strLength) which is 4 byte
+ length of value of param1 (str) which is strLength
+ length of param2 which is 4 byte
+ length of value of param2 (anz) which is 4 byte
+ NULL Byte (\0) which is 1 byte

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:

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

The last println will give me the following output:

packageLength875901497

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.

Help is appriciated. Thanks in advance.

Cheers Daniel

1

There are 1 answers

7
James M On BEST ANSWER
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;

You're converting the sizes to network order and then adding them together for packageLength. You should add them together before converting them to network order with htonl.

Apart from that, all this:

sprintf(byteBuffer, "%i%i%c%i%s%i%i%c", packageLength, byte, func, byte, str, byte, anz, '\0');

is wrong, because I assume you want to send them as binary, but using sprintf as above would send them in their base 10 ASCII representation (i.e. you're sending the numbers as text!)