Gamestudio Links
Zorro Links
Newest Posts
Free Live Data for Zorro with Paper Trading?
by AbrahamR. 05/18/24 05:41
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (AbrahamR, AndrewAMD), 1,278 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Hanky27, firatv, wandaluciaia, Mega_Rod, EternallyCurious
19051 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
send custom data #423288
05/27/13 15:01
05/27/13 15:01
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
Kartoffel Offline OP
Expert
Kartoffel  Offline 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:
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


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 Offline
Member
lemming  Offline
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.

Code:
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 Offline OP
Expert
Kartoffel  Offline 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? frown


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
W
Wiseguy Offline
Newbie
Wiseguy  Offline
Newbie
W

Joined: May 2013
Posts: 23
Somewhere
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.


His words are wise, his face is beard.
Re: send custom data [Re: Wiseguy] #423310
05/27/13 19:17
05/27/13 19:17
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
Kartoffel Offline OP
Expert
Kartoffel  Offline OP
Expert

Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
Thank you very much, I'll give it a try.


POTATO-MAN saves the day! - Random
Re: send custom data [Re: Kartoffel] #423349
05/28/13 10:30
05/28/13 10:30
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
Kartoffel Offline OP
Expert
Kartoffel  Offline OP
Expert

Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
I tried it but somehow I can't get it working smirk


POTATO-MAN saves the day! - Random
Re: send custom data [Re: Kartoffel] #423353
05/28/13 11:13
05/28/13 11:13
Joined: May 2013
Posts: 23
Somewhere
W
Wiseguy Offline
Newbie
Wiseguy  Offline
Newbie
W

Joined: May 2013
Posts: 23
Somewhere
Show your code and all error messages (and please, no screenshot) wink


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 Offline OP
Expert
Kartoffel  Offline OP
Expert

Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
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 )


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
W
Wiseguy Offline
Newbie
Wiseguy  Offline
Newbie
W

Joined: May 2013
Posts: 23
Somewhere
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.

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
W
Wiseguy Offline
Newbie
Wiseguy  Offline
Newbie
W

Joined: May 2013
Posts: 23
Somewhere
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.


His words are wise, his face is beard.
Page 1 of 2 1 2

Moderated by  HeelX, Spirit 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1