【问题标题】:ONC RPC Send a char in a struct from serverONC RPC 从服务器发送一个结构中的字符
【发布时间】:2016-05-13 11:41:07
【问题描述】:

当我从服务器向客户端发送一个字符串时遇到问题,它只发送一个带有数字“1”的字符

rpc.x

struct IdentIP{
    char *ip;
    int puerto;
};    
program SERVIDORCHAT {
    version BASICA {
        IdentIP query(string) = 3;
    } = 1;
} = 0x40001234;

查询功能的问题。

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);
}

我的第一个想法是我用 inet_ntoa 严重改变了格式,或者我没有节省足够的内存,但我尝试了所有但没有用。

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);

端口工作正常,但无论我做什么,ip 总是打印“1”。 我希望有人能找到错误,非常感谢你。

【问题讨论】:

    标签: c rpc xdr


    【解决方案1】:

    字符串没有复制到正确的结果缓冲区中:

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

    应该是:

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

    还可以更好地为 ip 分配 16 个字符的缓冲区,因为您需要为空终止符多保留一个字符。

    result.ip = malloc(16);
    

    【讨论】:

    • 翻译问题,抱歉
    • @mBayon 返回值呢?
    • 翻译也有问题,程序编译正常。 malloc(16) 不起作用,我认为问题与内存有关,但我尝试了所有我知道的组合。
    【解决方案2】:

    我发现了问题,在 xdr 中,您必须像动态字符串数组一样声明 char *。 .x 的好代码就是 this。

    rpc.x

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-27
      • 1970-01-01
      • 2012-07-27
      • 2015-10-20
      相关资源
      最近更新 更多