I have a problem when I send a string from the server to the client, it only sends a char with the number '1'.
rpc.x
struct IdentIP{
char *ip;
int puerto;
};
program SERVIDORCHAT {
version BASICA {
IdentIP query(string) = 3;
} = 1;
} = 0x40001234;
The problem its with the query function.
rpc.c
IdentIP * query_1_svc(char **nick, struct svc_req *r)
{
static IdentIP result;
result.port = -1;
Client *c;
int i = search_client(*nick);
if (i == -1) // No ha sido encontrado
return (&result);
c = clientes[i];
result.port = ntohs(c->endpoint->sin_port);
result.ip = malloc(sizeof(char)*15);
strcpy(result.ip,inet_ntoa(c->endpoint->sin_addr));
printf("IP: %s\n",result.ip); //HERE PRINTS THE IP CORRECTLY
return (&result);
}
My first though was that i change the format badly with the inet_ntoa or i didnt save memory enough, but i tryed all and doesnt work.
client.c
sscanf(linea, "%s %s", cmd, friend);
/***** Query al servidor del chat ****/
IdentIP *info_friend;
info_friend = query_1(&friend, clnt);
printf("IP: %s PUERTO: %d\n",(*info_friend).ip,(*info_friend).puerto);
//HERE PRINT "IP: 1" WHEN THE IP IS "192.168.1.103"
puerto_destino = (*info_friend).puerto;
strcpy(ip_destino, (*info_friend).ip);
The port works fine, but the ip always print '1' whatever i do. I hope someone can find the error, thank you so much beforehand.
The string is not copied into the correct result buffer:
should be:
Also better allocation the buffer for ip with 16 chars, as you need to reserve one more char for the null terminator.