send custom data

Posted By: Kartoffel

send custom data - 05/27/13 15:01

Hey there,

I'd like to send custom data (by using a struct and send_data_to)

I thought about something like this:
Code:
typedef struct MP_DATA
{
   var size; // size of 'data'
   int type; // what does 'data' contain
   void * data; // data
} MP_DATA;


Let's say I want to send an image.
I would set mp_data.size to the size of the image and copy the image into mp_data.data.
This should make it possible to use memcpy after receiving it.

...but that's just the theory and I have no idea how I shoud code this smirk

Could anyone give me a simple, working code example (if it's not too much work, of course)?
Sending and receiving a string with this method would already be enough.

regards, Kartoffel smile
Posted By: lemming

Re: send custom data - 05/27/13 18:19

As data is a Pointer it would point outside the struct and won't be sent.
You'd have to send the image itself as data.

But I really don't know how you difference if you get an image or something else, as the native system doesn't seem to support event-IDs.

For sending and recieving strings you'd have to implement a bunch of chars in your struct, like in the example in the manual.

Code:
typedef struct MYDATA {
  int x;
  char c[20];
} MYDATA;



edit:// As I recall, BMAP is ALSO just a struct with pointers.
Posted By: Kartoffel

Re: send custom data - 05/27/13 18:24

But I really don't know how you difference if you get an image or something else, as the native system doesn't seem to support event-IDs.

that's why theres the integer 'type' in the struct.

...is there really no way to store data in a struct and send this to another client? frown
Posted By: Wiseguy

Re: send custom data - 05/27/13 18:29

Code:
struct foo
{
    int type;
    size_t length;
};

// To allocate the struct
struct foo *bar = malloc(sizeof(foo) + theBytesINeed);

// To memcpy the data out of it:
void *buffer = ...;
memcpy(buffer, bar + 1, bar->length);



The code should be self explanatory. When sending, just append the extra bytes to the size of the struct, like the call of malloc() does.
Posted By: Kartoffel

Re: send custom data - 05/27/13 19:17

Thank you very much, I'll give it a try.
Posted By: Kartoffel

Re: send custom data - 05/28/13 10:30

I tried it but somehow I can't get it working smirk
Posted By: Wiseguy

Re: send custom data - 05/28/13 11:13

Show your code and all error messages (and please, no screenshot) wink
Posted By: Kartoffel

Re: send custom data - 05/28/13 19:37

Well I tried a lot of variations but nothing really worked grin
sometimes I got a 'crash in...' error, sometimes nothing happend and sometimes the engine just crashed (I guess I f*cked up the memcpy-thing)

heres the code as it is now.
's' starts a server, 'c' starts a client and enter sends the struct.

( and sorry for my stupidity... grin )
Posted By: Wiseguy

Re: send custom data - 05/28/13 20:06

Code:
memcpy(receive_str, input + 4, input.size);



Look at what I wrote! That was no mistake.
Please read up on pointer arithmetic as well.

Furthermore, the way you handle the STRING, is absolutely wrong! Use a cstring here, don't allocate memory for a STRING object, don't copy it around. You are doing it absolutely wrong in every possible way!

Edit: Nitpicking: Why do you declare a named struct when you never access it through the struct namespace anyways? Just use an anonymous struct for these things and avoid polluting the struct namespace.
Posted By: Wiseguy

Re: send custom data - 05/28/13 20:18

Here is the code in cleaned form:
http://hastebin.com/yikuxakegu.c

Notice, I used str_len(str) + 1 for the size. The null byte is important, though you usually don't send it with your string. If you append it on the receiving end, or just send it together with everything else, is up to you. Important is that the null byte is present.

In general it appears like you are missing some knowledge about pointers and memory allocations in general. I would urge you to fill these knowledge gaps before you end up with code that crashes all over the place and is hard to debug, or a program that just leaks memory like there is no tomorrow.
Posted By: Kartoffel

Re: send custom data - 05/28/13 20:46

Thanks you very much (again).

And yes, I haven't got very much knowledge / experience with memory allocations since I didn't needed stuff like this very often.
© 2024 lite-C Forums