send_data, problem with strings

Posted By: MrGuest

send_data, problem with strings - 02/11/10 05:41

Hi,

I'm having trouble sending a string from one PC to another using the send_data_to or send_data_id commands, I've already posted here but had no replies.

How exactly (needs to be using send_data not send_string as I need to send additional information with the string) do I go about this.

So far I've tried
typedef struct DATA {

int id;
int type;

// char name; //gives me a one char garbled message (as expected) from server to client
// char* name; //sends the correct message, but receives only 1 char, no chars, or crashes client
// char* name; //sending with _chr(msg.name), repeats the above, but never with a crash

// char name[20]; //sends unreadable string using _chr or _str
// char* name[20]; //sends unreadable string

STRING* name; //sends correct message, crashes client
//all messages shown for sending and receiving via update_log()
} DATA;

DATA msg;

here's the code I'm using
Code:
// MP_woes.c

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

typedef struct DATA {
	
	int id;
	int type;
	
//	char name; //gives me a one char garbled message (as expected) from server to client
//	char* name; //sends the correct message, but receives only 1 char, no chars, or crashes client
//	char* name; //sending with _chr(msg.name), repeats the above, but never with a crash 
	
//	char name[20]; //sends unreadable string using _chr or _str
//	char* name[20]; //sends unreadable string
	
	STRING* name; //sends correct message, crashes client
	
} DATA;

DATA msg;


#define DATA_SERVER_JOIN 1
#define DATA_SERVER_LEFT 2


#define players_max 8

int int_player_count;

var player_id[players_max];

TEXT* txt_players = {
	
	strings = 8;
	flags = SHOW;
}

TEXT* txt_eventlog = {
	
	pos_x = 200;
	strings = 40;
	flags = SHOW;
}


void set_names(STRING* str){
	
	(txt_players.pstring)[int_player_count] = str;
//	str_cpy((txt_players.pstring)[int_player_count], str);
	int_player_count++;
}

void get_names(){
	
}


void update_log(STRING* str){
	
/*	int i;
	for(i = 0; i < txt_eventlog.strings - 1; i++){
		
		str_cpy((txt_eventlog.pstring)[i], (txt_eventlog.pstring)[i+1]);
	}
	str_cpy((txt_eventlog.pstring)[txt_eventlog.strings - 1], str);
*/	int i;
	for(i = 0; i < txt_eventlog.strings - 1; i++){
		
		(txt_eventlog.pstring)[i] = (txt_eventlog.pstring)[i+1];
	}
	(txt_eventlog.pstring)[txt_eventlog.strings - 1] = str;
}


void client_event(void* str){
	
	update_log("CLIENT EVENT");
	
	switch(event_type){
		
		case EVENT_JOIN:
			
			update_log(" - EVENT JOIN");
			update_log("(me)");
			
			//update you when connection made
			set_names(player_name);
			
			//send client name
			
			
		break;
		
		case EVENT_LEAVE:
			
			update_log(" - EVENT LEAVE");
			update_log("(me)");
			
		break;
		
		case EVENT_VAR:
			
			update_log(" - EVENT VAR");
			
		break;
		
		case EVENT_STRING:
			
			update_log(" - EVENT STRING");
			
		break;
		
		case EVENT_DATA:
			
			update_log(" - EVENT DATA");
			
			memcpy(msg, str, sizeof(DATA));
			
			switch(msg.type){
				
				case DATA_SERVER_JOIN:
					
					update_log(" - DATA JOIN");
					
					set_names(msg.name);
					
				break;
				
				default:
				
					update_log(" - DATATYPE UNKNOWN");
				break;
			}
			
		break;
		
		default:
			
			update_log(" - EVENT UNKNOWN");
			
		break;
	}
}


void server_event(void* str, var id){
	
	update_log("SERVER EVENT");
	
	switch(event_type){
		
		case EVENT_JOIN:
			
			update_log(" - EVENT JOIN");
			update_log(str);
			
			//send current players to new player
			int i;
			for(i = 0; i < int_player_count; i++){
				
				msg.id = id;
				msg.type = 1;
				msg.name = "STEVE";//_chr((txt_players.pstring)[i]);
				
				send_data_id(id, msg, sizeof(DATA));
				
				update_log(" - SENT -");
				update_log(msg.name);
			}
			
			//add to server
			set_names(str);
			
			//add me to player list
			
		break;
		
		case EVENT_LEAVE:
			
			update_log(" - EVENT LEAVE");
			
		break;
		
		case EVENT_VAR:
			
			update_log(" - EVENT VAR");
			
		break;
		
		case EVENT_STRING:
			
			update_log(" - EVENT STRING");
			
		break;
		
		case EVENT_DATA:
			
			update_log(" - EVENT DATA");
			
		break;
	}
	
}

void main(){
	
	int i;
	for(i = 0; i < txt_eventlog.strings; i++){
//		(txt_eventlog.pstring)[i] = str_create("");
	}
	
	on_client = client_event;
	on_server = server_event;
	
	wait(1);
	
	//if host
	if(connection == 3){
		
		set_names(player_name);
	}
	
	//if server / host
	if(connection & 1){
		
	}
	
	//if client / host
	if(connection & 2){
		
	}
	
	update_log("MAIN");
	
}

any insight would be appreciated

NB: you can probably already see I've tried using str_create, str_cpy for the log, but unsure which if i should be using = or str_cpy, both seem to have advantages and disadvantages.

I am able to workaround atm by doing string manipulation before sending and after receving using send_string, but would prefer to use the send_data method.

Many thanks
MrGuest
Posted By: jcl

Re: send_data, problem with strings - 02/11/10 08:36

I'm not sure why you want to use send_data, but your code makes no sense to me. For instance, you can not send pointers over the network. Pointers point to data in memory and are meaningless when there is no data. You must put all info into the struct you're sending.

Commands like send_data are for us nerds only. For using them you must understand programming on a level beyond the lite-C workshops - f.i. by reading a "real" C/C++ tutorial. For sending simple things like names or variables, just use the string and var send commands.
Posted By: MrGuest

Re: send_data, problem with strings - 02/12/10 11:42

The pointers were just trial and error for sending before saving to the struct.

I want to be able to send data to the client, which consists of who sent the data originally, what type of data it is (chat/creating a game/a model name/etc), and the string/char. I can achieve this using string manipulation, but if it's possible via send_data, I'd like to know and how it's possible to achieve this.

Thanks again
MrGuest

EDIT:
Revised question:
How do I get the msg.name displayed as a string? (must still be stored as a char array for sending purposes later)
Code:
// MP_woes.c

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

typedef struct DATA {
	
	int id;
	int type;
	
	char name[20]; //sends unreadable string using _chr or _str
	
} DATA;

DATA msg;

TEXT* txt_eventlog = {
	
	pos_x = 200;
	strings = 40;
	flags = SHOW;
}


void update_log(STRING* str){
	
/*	int i;
	for(i = 0; i < txt_eventlog.strings - 1; i++){
		
		str_cpy((txt_eventlog.pstring)[i], (txt_eventlog.pstring)[i+1]);
	}
	str_cpy((txt_eventlog.pstring)[txt_eventlog.strings - 1], str);
*/	
	int i;
	for(i = 0; i < txt_eventlog.strings - 1; i++){
		
		(txt_eventlog.pstring)[i] = (txt_eventlog.pstring)[i+1];
	}
	(txt_eventlog.pstring)[txt_eventlog.strings - 1] = str;
}


void main(){
	
	int i;
	for(i = 0; i < txt_eventlog.strings; i++){
//		(txt_eventlog.pstring)[i] = str_create("");
	}
	
	update_log("MAIN");
	
	msg.name = "FRED";
	update_log(msg.name);
}


Posted By: FBL

Re: send_data, problem with strings - 02/12/10 13:29

msg.name = "FRED";

shouldn't this be str_cpy(msg.name, "FRED");?
Posted By: MrGuest

Re: send_data, problem with strings - 02/12/10 16:21

nope, gives error 1515 in main, as msg.name is char data type

Thanks for the reply though laugh
Posted By: FBL

Re: send_data, problem with strings - 02/12/10 16:26

Then use strcpy() from the standard c lib and use &msg.name as parameter.
Posted By: MrGuest

Re: send_data, problem with strings - 02/12/10 17:36

Then surely I'll need to rewrite the update_log to support chars too?

Just tested, currently the strings are just garbage, thanks again for the reply
Posted By: FBL

Re: send_data, problem with strings - 02/12/10 17:38

str_cpy can copy either STRING* to STRING* or char* to STRING*, so you should be fine there.

This however:
(txt_eventlog.pstring)[i] = (txt_eventlog.pstring)[i+1];
should be done with str_cpy()...

Maybe it also works your way, but I'm not sure whether it is good to bend around the pointers of a TEXT* object...
Also by default when giving a 'strings' number during definition of the TEXT* the strings will already be created, so you don't need str_create. This will just leave garbage in the memory.... if I understood the internals of a TEXT* object correctly...

jcl can answer this better than me I guess...
Posted By: MrGuest

Re: send_data, problem with strings - 02/12/10 17:59

just a quick test,

str_cpy() after strcpy releases an invalid pointer and crashes on exit, error W1516

though if use = instead of str_cpy after the initial c lib strcpy() then it works

there is no need to use & to pass the pointer to a variable, strcpy obviously achieves this

Many thanks Firoball! laugh
MrGuest
Posted By: MMike

Re: send_data, problem with strings - 02/22/10 01:37

when you do msg.name="ehhehe"
i think all the array will be name= hehe right?
or you need to index it?

msg.name[0]="ehhe";???
© 2024 lite-C Forums