简体   繁体   English

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

[英]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'. 我从服务器向客户端发送字符串时遇到问题, 它仅发送数字“ 1” 的字符

rpc.x 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 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);
}

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. 不过,我的第一个建议是,我使用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);

The port works fine, but the ip always print '1' whatever i do. 端口工作正常,但是无论我做什么,ip都始终显示“ 1”。 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. 另外,最好为ip分配16个字符的缓冲区,因为您需要为null终止符再保留一个字符。

result.ip = malloc(16);

I find the problem, in xdr you have to declare the char * like a dynamic array of string. 我发现了问题,在xdr中必须将char *声明为动态字符串数组。 The good code of the .x its this. .x的好代码是这个。

rpc.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.

相关问题 如何在onc-rpc中将字符串(char *)从服务器发送到客户端 - How te send a string (char*) from server to client in onc-rpc 从Linux连接到Sun ONC RPC服务器 - Connect to Sun ONC RPC server from Linux 使用SUN-RPC从客户端发送结构并保存到链接列表中的服务器 - Send struct from client and save to server in linked list with SUN-RPC IN onc RPC,哪些是服务器文件,哪些是客户端文件? 文件的意义是什么? - IN onc RPC , which are the server files and which are the client files? what is significance of files? 在ONC RPC中,从两个线程调用svc_run()是否有效,使用不同的程序注册? - In ONC RPC, is it valid to call svc_run() from two threads, registered with different program no? 在MPI中使用char []发送结构 - Send struct with char[] in MPI 从服务器向客户端发送带有sun rpc的结构数组 - sending a struct array with sun rpc from server to client 从多线程RPC服务器返回带有字符串的结构 - Return a struct with string from a multi threaded RPC server NSString *转换为Char [](GameCenter发送结构) - NSString * to Char[] (GameCenter Send Struct) SUN RPC(ONC / RPC):在C中使用空过程计算往返时间(或ping) - SUN RPC (ONC/RPC): Calculating round trip time (or pinging) using null procedure in C
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM