简体   繁体   中英

ONC RPC Send a char in a struct from server

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:

strcpy(resultado.ip, inet_ntoa(c->endpoint->sin_addr));
return (&resultado);

should be:

strcpy(result.ip, inet_ntoa(c->endpoint->sin_addr));
return (&result);

Also better allocation the buffer for ip with 16 chars, as you need to reserve one more char for the null terminator.

result.ip = malloc(16);

I find the problem, in xdr you have to declare the char * like a dynamic array of string. The good code of the .x its this.

rpc.x

struct IdentIP{
    string ip<>; //HERE ITS THE CHANGE
    int puerto;
};    
program SERVIDORCHAT {
    version BASICA {
        IdentIP query(string) = 3;
    } = 1;
} = 0x40001234;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM