Gamestudio Links
Zorro Links
Newest Posts
AlpacaZorroPlugin v1.3.0 Released
by kzhao. 05/19/24 18:45
Free Live Data for Zorro with Paper Trading?
by AbrahamR. 05/18/24 13:28
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
3 registered members (7th_zorro, AndrewAMD, TedMar), 837 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
send_data, problem with strings #309779
02/11/10 05:41
02/11/10 05:41
Joined: Jul 2008
Posts: 1,178
England
M
MrGuest Offline OP
Serious User
MrGuest  Offline OP
Serious User
M

Joined: Jul 2008
Posts: 1,178
England
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

Re: send_data, problem with strings [Re: MrGuest] #309790
02/11/10 08:36
02/11/10 08:36
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,986
Frankfurt
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.

Re: send_data, problem with strings [Re: jcl] #309967
02/12/10 11:42
02/12/10 11:42
Joined: Jul 2008
Posts: 1,178
England
M
MrGuest Offline OP
Serious User
MrGuest  Offline OP
Serious User
M

Joined: Jul 2008
Posts: 1,178
England
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);
}



Last edited by MrGuest; 02/12/10 12:50.
Re: send_data, problem with strings [Re: MrGuest] #309982
02/12/10 13:29
02/12/10 13:29
Joined: Sep 2003
Posts: 9,859
F
FBL Offline
Senior Expert
FBL  Offline
Senior Expert
F

Joined: Sep 2003
Posts: 9,859
msg.name = "FRED";

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

Re: send_data, problem with strings [Re: FBL] #310006
02/12/10 16:21
02/12/10 16:21
Joined: Jul 2008
Posts: 1,178
England
M
MrGuest Offline OP
Serious User
MrGuest  Offline OP
Serious User
M

Joined: Jul 2008
Posts: 1,178
England
nope, gives error 1515 in main, as msg.name is char data type

Thanks for the reply though laugh

Re: send_data, problem with strings [Re: MrGuest] #310010
02/12/10 16:26
02/12/10 16:26
Joined: Sep 2003
Posts: 9,859
F
FBL Offline
Senior Expert
FBL  Offline
Senior Expert
F

Joined: Sep 2003
Posts: 9,859
Then use strcpy() from the standard c lib and use &msg.name as parameter.

Re: send_data, problem with strings [Re: FBL] #310020
02/12/10 17:36
02/12/10 17:36
Joined: Jul 2008
Posts: 1,178
England
M
MrGuest Offline OP
Serious User
MrGuest  Offline OP
Serious User
M

Joined: Jul 2008
Posts: 1,178
England
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

Re: send_data, problem with strings [Re: MrGuest] #310022
02/12/10 17:38
02/12/10 17:38
Joined: Sep 2003
Posts: 9,859
F
FBL Offline
Senior Expert
FBL  Offline
Senior Expert
F

Joined: Sep 2003
Posts: 9,859
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...

Last edited by Firoball; 02/12/10 17:45.
Re: send_data, problem with strings [Re: FBL] #310027
02/12/10 17:59
02/12/10 17:59
Joined: Jul 2008
Posts: 1,178
England
M
MrGuest Offline OP
Serious User
MrGuest  Offline OP
Serious User
M

Joined: Jul 2008
Posts: 1,178
England
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

Re: send_data, problem with strings [Re: MrGuest] #311887
02/22/10 01:37
02/22/10 01:37
Joined: Jul 2004
Posts: 1,710
MMike Offline
Serious User
MMike  Offline
Serious User

Joined: Jul 2004
Posts: 1,710
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";???


Moderated by  old_bill, Tobias 

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