1 registered members (7th_zorro),
1,390
guests, and 2
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
send custom data
#423288
05/27/13 15:01
05/27/13 15:01
|
Joined: Jun 2009
Posts: 2,210 Bavaria, Germany
Kartoffel
OP
Expert
|
OP
Expert
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
|
Hey there, I'd like to send custom data (by using a struct and send_data_to) I thought about something like this:
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 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
POTATO-MAN saves the day! - Random
|
|
|
Re: send custom data
[Re: Kartoffel]
#423302
05/27/13 18:19
05/27/13 18:19
|
Joined: Nov 2011
Posts: 274 de
lemming
Member
|
Member
Joined: Nov 2011
Posts: 274
de
|
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.
typedef struct MYDATA {
int x;
char c[20];
} MYDATA;
edit:// As I recall, BMAP is ALSO just a struct with pointers.
Last edited by lemming; 05/27/13 18:23.
|
|
|
Re: send custom data
[Re: lemming]
#423303
05/27/13 18:24
05/27/13 18:24
|
Joined: Jun 2009
Posts: 2,210 Bavaria, Germany
Kartoffel
OP
Expert
|
OP
Expert
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
|
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?
POTATO-MAN saves the day! - Random
|
|
|
Re: send custom data
[Re: Kartoffel]
#423305
05/27/13 18:29
05/27/13 18:29
|
Joined: May 2013
Posts: 23 Somewhere
Wiseguy
Newbie
|
Newbie
Joined: May 2013
Posts: 23
Somewhere
|
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.
His words are wise, his face is beard.
|
|
|
Re: send custom data
[Re: Wiseguy]
#423369
05/28/13 19:37
05/28/13 19:37
|
Joined: Jun 2009
Posts: 2,210 Bavaria, Germany
Kartoffel
OP
Expert
|
OP
Expert
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
|
Well I tried a lot of variations but nothing really worked 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... )
POTATO-MAN saves the day! - Random
|
|
|
Re: send custom data
[Re: Kartoffel]
#423375
05/28/13 20:06
05/28/13 20:06
|
Joined: May 2013
Posts: 23 Somewhere
Wiseguy
Newbie
|
Newbie
Joined: May 2013
Posts: 23
Somewhere
|
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.
Last edited by Wiseguy; 05/28/13 20:13.
His words are wise, his face is beard.
|
|
|
Re: send custom data
[Re: Wiseguy]
#423377
05/28/13 20:18
05/28/13 20:18
|
Joined: May 2013
Posts: 23 Somewhere
Wiseguy
Newbie
|
Newbie
Joined: May 2013
Posts: 23
Somewhere
|
Here is the code in cleaned form: http://hastebin.com/yikuxakegu.cNotice, 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.
His words are wise, his face is beard.
|
|
|
|