簡體   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