繁体   English   中英

ONC RPC从服务器以结构形式发送字符

[英]ONC RPC Send a char in a struct from server

我从服务器向客户端发送字符串时遇到问题, 它仅发送数字“ 1” 的字符

rpc.x

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

问题与查询功能有关。

rpc

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严重更改了格式,或者我没有节省足够的内存,但是我尝试了所有操作,但无法正常工作。

客户端

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”。 希望有人能找到错误,在此先谢谢您。

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

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

应该:

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

另外,最好为ip分配16个字符的缓冲区,因为您需要为null终止符再保留一个字符。

result.ip = malloc(16);

我发现了问题,在xdr中必须将char *声明为动态字符串数组。 .x的好代码是这个。

rpc.x

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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