|
|
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
OP
Senior Member
|
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:
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:
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: TechMuc]
#403132
06/14/12 20:03
06/14/12 20:03
|
Joined: Nov 2007
Posts: 318 Brasil, Paraná
NeoNeper
OP
Senior Member
|
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]
#403162
06/15/12 15:21
06/15/12 15:21
|
Joined: Nov 2007
Posts: 318 Brasil, Paraná
NeoNeper
OP
Senior Member
|
OP
Senior Member
Joined: Nov 2007
Posts: 318
Brasil, Paraná
|
On Server:
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
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:
#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"
str_cpy(mydata.nome[0],"Mike");//Error
If I change the initialization of the structure to:
#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
OP
Senior Member
|
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.
typedef struct MYDATA {
int id;
STRING* name;
}MYDATA;
Or
typedef struct MYDATA {
int id;
char* name;
}MYDATA;
or
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
OP
Senior Member
|
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
TechMuc
User
|
User
Joined: Jul 2008
Posts: 894
|
I only had a short look at your first example but this:
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
OP
Senior Member
|
OP
Senior Member
Joined: Nov 2007
Posts: 318
Brasil, Paraná
|
Hy TechMuc. Tanks for response. //The server will receive a data packet
//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 ​​of writing, even if using "STRING *" are not correctly sender, or received! Function that sends data packets into client
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. _______________________________________________________
|
|
|
|