-2

I'm wanting to piece together a binary string before sending it over UDP. This will be running in C on very small linux computers. I'm hoping there exists some way to do this in C similar to how I'd use a BinaryWriter to do this in C#. I'm hoping to be able to push an int as 4 bytes onto the end of the char array, push all of another string onto the end, or a short as 2 bytes, etc.

I don't know C very well and anytime I google something, C++ always comes up but I can't get vector or stringstream (sstream?) to compile. It simply says they're undeclared.

How I'm compiling:

gcc -o test test.c

I need to have a char* all built before I pass it to sendto where I'm sending over the UDP port.

Corey Ogburn
  • 24,072
  • 31
  • 113
  • 188
  • 1
    C != C++, so a C++ program won't compile with a C compiler. – Fred Foo Oct 31 '13 at 20:12
  • [`malloc(3)`](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong), [`realloc(3)`](http://pubs.opengroup.org/onlinepubs/009604499/functions/realloc.html), [`free(3)`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/free.html), [`memcpy(3)`](http://pubs.opengroup.org/onlinepubs/009695399/functions/memcpy.html) –  Oct 31 '13 at 20:13
  • What's the C compiler then? – Corey Ogburn Oct 31 '13 at 20:14
  • 1
    @CoreyOgburn The C compiler you are using is GCC. Don't try to compile C++ code with it, because it won't work (nor would a C program compile as C++, because they are different languages). C++ classes and other features are not available in C, just like you can't say that you would like to use the word "sans-abri" in English, because it's a French word. You have to say "homeless" instead. –  Oct 31 '13 at 20:17
  • I'm trying to use the C Compiler. I didn't realize that vector and stringstream were exclusively C++. – Corey Ogburn Oct 31 '13 at 20:20
  • C is out of my comfort zone. That's why I ask questions. – Corey Ogburn Oct 31 '13 at 20:22
  • "I didn't realize that vector and stringstream were exclusively C++" As a general rule (the buffs who are good at both C and C++ can probably explain this better, I'm actually someone who knows mostly C and little C++), if the header you needed to `#include` isn't prefaced with `c` (like `cstdio` or `cstring`), then it's probably exclusively C++. – Dennis Meng Nov 02 '13 at 21:49

1 Answers1

0

You can create a buffer, then walk the array as you fill it in. If you want to put in an int, you can cast the pointer or use memcpy() and then after writing, increment you walker (pointer) by the size of the thing written. The difficulty would then be in reading it out as the correct sizes, but if your order is proscribed by some protocol (maybe you decide) then you can recover it. For example:

void* buffer = malloc(1500);
void* walker = buffer;
uint32_t myVal = htonl(31415);
memcpy(walker, &myVal, sizeof(myVal);
walker += sizeof(myVal);

Also note that you will need to ensure that the sender and receiver for this type of scenario are share an endian scheme (both big endian or little endian [or convert]). This can be helped by using functions like ntohs() and htons() which convert number between a host specific format, and a net neutral version.

Andrew Ring
  • 3,185
  • 1
  • 23
  • 28
  • `*(int*)walker` violates the strict aliasing rule, and as such, it invokes undefined behavior. –  Oct 31 '13 at 20:17