Gamestudio Links
Zorro Links
Newest Posts
zorro license, IB connection
by miwok. 12/06/23 16:32
Newbie Questions
by fairtrader. 12/06/23 11:29
Zorro Trader GPT
by TipmyPip. 12/04/23 11:34
Square root rule
by Smallz. 12/02/23 09:15
RTest not found error
by TipmyPip. 12/01/23 21:43
neural function for Python to [Train]
by TipmyPip. 12/01/23 14:47
Xor Memory Problem.
by TipmyPip. 11/28/23 14:23
Training with command line parameters
by TipmyPip. 11/26/23 08:42
AUM Magazine
Latest Screens
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Tactics of World War I
Who's Online Now
6 registered members (miwok, AndrewAMD, TipmyPip, 3run, Quad, 1 invisible), 645 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
fairtrader, hus, Vurtis, Harry5, KelvinC
19019 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
How to send and receive data (char *) with send_data? #403108
06/14/12 15:08
06/14/12 15:08
Joined: Nov 2007
Posts: 318
Brasil, Paraná
NeoNeper Offline OP
Senior Member
NeoNeper  Offline OP
Senior Member

Joined: Nov 2007
Posts: 318
Brasil, Paraná
Already I posted this and Also Other issues like this, However, Until Now mE.m Helped no one helped me.


I'm not having success in the receipt of characters (char*), using the function ("send_data");

Below is an example of how I'm trying:

On Server:
Code:
typedef struct MYDATA {
  int id;
  char name[20];
} MYDATA;


MYDATA* mydata = { id = 0; name = ""; }	

function server_event(STRING* name, var idd)
{

	if (event_type == EVENT_JOIN)
	{
	//send_data for all Clients
        mydata.name="Mike";
        mydata.id=10;
	send_data_to(NULL,mydata,sizeof(MYDATA));
	}
}



In the example described above, I Sending a data packet to all Clients
The data packet sends a whole number ("ID") and a sequence of characters ("name")
>Now I'll describe how I'm working on the "Client" to receive this data packet

On Clients:

Code:
typedef struct MYDATA {
  int id;
  char name[20];
} MYDATA;


STRING* mystring = "";
TEXT* tMessage = { string(mystring); flags |= SHOW; layer = 1;  pos_x = 10;  pos_y = 50;  font = "arial#16b"; }

MYDATA* mydata = { id = 0; name = ""; }	

function client_event(void* buffer)
{

 if(event_type == EVENT_DATA){
 	
 	
 	memcpy(mydata,buffer,sizeof(MYDATA));
 	str_cat((tMessage.pstring)[0],str_for_num(NULL,mydata.id));
 	str_cat((tMessage.pstring)[0]," / ");
 	str_cat((tMessage.pstring)[0],mydata.name);
 	
 	}
}




There are the practical examples that show how I am sending a data packet from the server to the client, and also how I'm getting this information into Client.

In testing what happens is:
The value (int id) is being sent and received successfully
But I'm not having the same success with the data (char* name)
When I go to print on screen the result of the data packet received, the value (int id) is correct, but the value (char * name) is not correct. It comes empty or comes with a lot of unknown characters .

PS: It is worth remembering that I'm using an unprotect connection. (*)
Sample:
session_open("*test")
And
session_connect("*test","localhost");

Can anyone help me?



Last edited by NeoNeper; 06/14/12 15:10.

Please! Use easy words to be translated. because my English is not very good! Grateful.
_______________________________________________________
Re: How to send and receive data (char *) with send_data? [Re: NeoNeper] #403111
06/14/12 15:37
06/14/12 15:37
Joined: Jul 2008
Posts: 894
T
TechMuc Offline
User
TechMuc  Offline
User
T

Joined: Jul 2008
Posts: 894
You can not assign a string to an array of chars. You'll have to use strcpy (or strcpy_s).

Code:
char _test[20];
   strcpy(_test[0],"Mike");
   error(_test);



Re: How to send and receive data (char *) with send_data? [Re: TechMuc] #403132
06/14/12 20:03
06/14/12 20:03
Joined: Nov 2007
Posts: 318
Brasil, Paraná
NeoNeper Offline OP
Senior Member
NeoNeper  Offline OP
Senior Member

Joined: Nov 2007
Posts: 318
Brasil, Paraná
When I create a char * pointer outside a "struct", I actually use the example you cited above.

But when I'm working with a (char *) within a "struct" I do not have success using this same example.


Please! Use easy words to be translated. because my English is not very good! Grateful.
_______________________________________________________
Re: How to send and receive data (char *) with send_data? [Re: NeoNeper] #403151
06/15/12 08:14
06/15/12 08:14
Joined: Jul 2000
Posts: 27,967
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,967
Frankfurt
Your example looks correct, except of course for the wrong struct initialization mentioned by TechMuc.

When a program receives wrong data, the first thing to do is checking if you've sent the correct data at all.

Re: How to send and receive data (char *) with send_data? [Re: NeoNeper] #403162
06/15/12 15:21
06/15/12 15:21
Joined: Nov 2007
Posts: 318
Brasil, Paraná
NeoNeper Offline OP
Senior Member
NeoNeper  Offline OP
Senior Member

Joined: Nov 2007
Posts: 318
Brasil, Paraná
On Server:
Code:
typedef struct MYDATA {
  int id;
  char name[20];
} MYDATA;


MYDATA* mydata = { id = NULL; name = NULL; }	

function server_event(STRING* name, var idd)
{

	if (event_type == EVENT_JOIN)
	{
	//send_data for all Clients
        str_cpy(mydata.name[0],"Mike");
        mydata.id=10;
	send_data_to(NULL,mydata,sizeof(MYDATA));
	}
}



ON CLIENT
Code:
typedef struct MYDATA {
  int id;
  char name[20];
} MYDATA;


STRING* mystring = "";
TEXT* tMessage = { string(mystring); flags |= SHOW; layer = 1;  pos_x = 10;  pos_y = 50;  font = "arial#16b"; }

MYDATA* mydata = { id = NULL; name = NULL; }	

function client_event(void* buffer)
{

 if(event_type == EVENT_DATA){
 	
 	
 	memcpy(mydata,buffer,sizeof(MYDATA));
 	str_cat((tMessage.pstring)[0],str_for_num(NULL,mydata.id));
 	str_cat((tMessage.pstring)[0]," / ");
 	str_cat((tMessage.pstring)[0],mydata.name[0]);
 	
 	}
}



See I adapted the example I had posted earlier, to make it according to the reference of the TechMuc!

However when I run, I get a message of "(Invalid argument in server_event)", or a message of "(script crash in main)."

But the value (int ID) is transmitted correctly!

Now another simpler example, which shows only the failure to assign a value to index an array of char * within a structure:

Code:
#include <acknex.h>
#include <default.c>

STRING* mystring = "Just some string";

typedef struct MYDATA {
  int id;
  char nome[20];
}MYDATA;

STRING* server_text ="";

TEXT* tMessage = { 
string(mystring); 
flags |= SHOW;
 layer = 1;
  pos_x = 10;
  pos_y = 50;
  font = "arial#16b";
  
 }
 
 	MYDATA* mydata = {id = NULL; nome = NULL;}

function main()
{

	
mydata.id =1;
str_cpy(mydata.nome[0],"Mike");
str_cpy((tMessage.pstring)[0],str_for_num(NULL,mydata.id));
str_cat((tMessage.pstring)[0],"\n");
str_cat((tMessage.pstring)[0],mydata.nome[0]);
	


}



error: "Invalid function argument in Main()"
This error just Occurs When I assign a value to the index of the char *, using the "str_cpy"

Code:
str_cpy(mydata.nome[0],"Mike");//Error



If I change the initialization of the structure to:
Code:
#define new(type) malloc(sizeof(type##))
...
MYDATA* mydata=new(MYDATA);
...
str_cpy(mydata.nome[0],"Mike");//Error crash script in main


The error is returned "... crash script in main"...

Please: Could you explain, how to do this in a correct manner??
Tanks friends.





Last edited by NeoNeper; 06/15/12 15:48.

Please! Use easy words to be translated. because my English is not very good! Grateful.
_______________________________________________________
Re: How to send and receive data (char *) with send_data? [Re: NeoNeper] #403209
06/16/12 14:34
06/16/12 14:34
Joined: Nov 2007
Posts: 318
Brasil, Paraná
NeoNeper Offline OP
Senior Member
NeoNeper  Offline OP
Senior Member

Joined: Nov 2007
Posts: 318
Brasil, Paraná
Friends! I'm really struggling to use the "send_data_to"
to send values "char *" or "Strings".
I already ententei of all possible ways I know and are not working.

Code:
typedef struct MYDATA {
	int id;
	STRING* name;
}MYDATA;


Or
Code:
typedef struct MYDATA {
	int id;
	char* name;
}MYDATA;



or

Code:
typedef struct MYDATA {
	int id;
	char name[26];
}MYDATA;



My real intention at the moment is to send and receive written values;.
But of all the ways I tried, Or these values are not being properly sender, or else are not being received correctly.

Please could you show me a practical example that works?
Because the documentation is not addressing this issue in detail, and the way it is, is not working!..





Last edited by NeoNeper; 06/16/12 14:37.

Please! Use easy words to be translated. because my English is not very good! Grateful.
_______________________________________________________
Re: How to send and receive data (char *) with send_data? [Re: NeoNeper] #403291
06/17/12 19:39
06/17/12 19:39
Joined: Nov 2007
Posts: 318
Brasil, Paraná
NeoNeper Offline OP
Senior Member
NeoNeper  Offline OP
Senior Member

Joined: Nov 2007
Posts: 318
Brasil, Paraná
Nobody can help me?
I kept trying, but still could function!

Last edited by NeoNeper; 06/17/12 19:40.

Please! Use easy words to be translated. because my English is not very good! Grateful.
_______________________________________________________
Re: How to send and receive data (char *) with send_data? [Re: NeoNeper] #403329
06/18/12 12:23
06/18/12 12:23
Joined: Jul 2008
Posts: 894
T
TechMuc Offline
User
TechMuc  Offline
User
T

Joined: Jul 2008
Posts: 894
I only had a short look at your first example but this:

Code:
typedef struct MYDATA {
  int id;
  char name[20];
} MYDATA;


MYDATA* mydata = { id = NULL; name = NULL; }	

function server_event(STRING* name, var idd)
{

	if (event_type == EVENT_JOIN)
	{
	//send_data for all Clients
        str_cpy(mydata.name[0],"Mike");
        mydata.id=10;
	send_data_to(NULL,mydata,sizeof(MYDATA));
	}
}



will again not work. mydata.name[0] will probably be corrupted after the str_cpy instruction. Str_cpy needs a STRING pointer. You're argument (mydata.name[0]) is a char pointer. You'll have to use strcpy(mydata.name[0],"Mike"). (NO underscore).

So: You can NOT use 3D-Gamestudio String instructions on simple C Char Arrays. You'll have to use C-String Manipulation instructions (strcpy, strcat, ...).

Greetings,
Timo

Last edited by TechMuc; 06/18/12 12:24.
Re: How to send and receive data (char *) with send_data? [Re: TechMuc] #403332
06/18/12 13:00
06/18/12 13:00
Joined: Nov 2007
Posts: 318
Brasil, Paraná
NeoNeper Offline OP
Senior Member
NeoNeper  Offline OP
Senior Member

Joined: Nov 2007
Posts: 318
Brasil, Paraná
Hy TechMuc. Tanks for response.

//The server will receive a data packet

Code:
//define macro new
#define new(type) malloc(sizeof(type##))

typedef struct MYDATA {
  int id;
  STRING* name;
} MYDATA;




function server_event(void* buffer)
{

	if (event_type == EVENT_DATA)
	{
	
        //Start Struct
        MYDATA* mydata = new(MYDATA);
	mydata.id=0;
	mydata.name=str_create("");
	//Copy struct
        memcpy(mydata,buffer,sizeof(MYDATA));
	str_cpy((server_txt.pstring)  [0],str_for_num(NULL,mydata.id)); //Sucefull
	str_cat((server_txt.pstring)[0],"\n");	
	str_cat((server_txt.pstring)[0],mydata.name); //No Sucefull



	}
}


Note that I modified the script, replacing the "char name []" for "STRING * name;"
This is a functional script. However, when using the send_data to send this packet, the data in (STRING *) are lost.

When I capture the packets sent, usually I get any numeric value. but the values &#8203;&#8203;of writing, even if using "STRING *" are not correctly sender, or received!

Function that sends data packets into client

Code:
MYDATA* mydata = new(MYDATA);
	mydata.id = 100;
	mydata.name= str_create("");	
	str_cpy(mydata.name,"julio");	
	send_data_to(NULL,mydata,sizeof(MYDATA));



Note how I'm doing to send the packet!

This is a valid function.
But this data in STRING * is not being sent correctly!
Or, these data are being sent correctly, but I am not using a proper way to capture this data in "STRING *"
And there is also the possibility that we are seeing a bug enguine!



Last edited by NeoNeper; 06/19/12 12:48.

Please! Use easy words to be translated. because my English is not very good! Grateful.
_______________________________________________________
Re: How to send and receive data (char *) with send_data? [Re: TechMuc] #403333
06/18/12 13:03
06/18/12 13:03
Joined: Jul 2000
Posts: 27,967
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,967
Frankfurt
You seem to have a real talent to initialize a char array in more wrong ways than I thought possible, instead of just writing

strcpy(mydata.name,"Mike");

Programming does not work by trial and error. You must know how to use arrays and pointers when you have them in your code. Asking for advices makes only sense when you can understand the answers. There are several free C online courses, such as Sam's Teach Yourself C in 21 Days - they cover arrays and string functions.

Page 1 of 2 1 2

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

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