0

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

Daniel
  • 157
  • 2
  • 12

1 Answers1

2
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!)

James M
  • 18,506
  • 3
  • 48
  • 56
  • This was some trial and error pieces i did. But i changed it again and i get like this uint32_t packageLength, funcLength, strLength; funcLength = sizeof(func); strLength = strlen(str); anz = htonl(anz); packageLength = 4 + 4 + funcLength + 4 + strLength + 4 + 4 + 1; /*packageLength = htonl(packageLength);*/ strLength = htonl(strLength); funcLength = htonl(funcLength); uint32_t byte = htonl(4); uint32_t trailing = htonl(1); char byteBuffer[packageLength]; packageLength = htonl(packageLength); – Daniel Jan 22 '12 at 12:31
  • In Java i had a ByteBuffer were i put the data, which had given me the methods .putInt() .put() to put things in a bytebuffer and then send the ByteBuffer. I didn't know a way to pack my data as to put it in an char array. – Daniel Jan 22 '12 at 12:38
  • Nonetheless i get on server-side with changed code (see above) the same packageLength: packageLength875901497 How can i tell c to write my data in binary to the socket? – Daniel Jan 22 '12 at 12:39
  • *(int *) (byteBuffer + _offset_) = some_int; – James M Jan 22 '12 at 12:41
  • And something like memcpy for strings – James M Jan 22 '12 at 12:41
  • I don't understand this line of code sorry. what's the matter with that *(int *) in front of it? – Daniel Jan 22 '12 at 12:44
  • Dereferencing part of the buffer as an int. Perhaps you should read a C book? – James M Jan 22 '12 at 12:46