How to send and receive data (char *) with send_data?

Posted By: NeoNeper

How to send and receive data (char *) with send_data? - 06/13/12 13:05

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 .



Can anyone help me?

Posted By: Espér

Re: How to send and receive data (char *) with send_data? - 06/13/12 13:13

perhaps you've written 'nome' instead of 'name' ?
Posted By: NeoNeper

Re: How to send and receive data (char *) with send_data? - 06/13/12 13:17

Ohh No No. Sory.
It was a typo here!
In the script are all instantiated as "name".

But Thanks for your attention (^.^).
I fix the Previous Post!

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

© 2023 lite-C Forums