|
send_data, problem with strings
#309779
02/11/10 05:41
02/11/10 05:41
|
Joined: Jul 2008
Posts: 1,178 England
MrGuest
OP
Serious User
|
OP
Serious User
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
// 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: jcl]
#309967
02/12/10 11:42
02/12/10 11:42
|
Joined: Jul 2008
Posts: 1,178 England
MrGuest
OP
Serious User
|
OP
Serious User
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)
// 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]
#310022
02/12/10 17:38
02/12/10 17:38
|
Joined: Sep 2003
Posts: 9,859
FBL
Senior Expert
|
Senior Expert
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
MrGuest
OP
Serious User
|
OP
Serious User
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! MrGuest
|
|
|
|