1

文字列を引数として取り、その長さを呼び出しプログラムに返す単純な RPC プログラムを作成しようとしています。必要なスタブを生成するために RPCGEN を使用しています。コードは次のとおりです。 prog.x ファイル:

    program PROGRAM_PROG {
    version PROGRAM_VERS {
    int fun(string) = 1;    /* procedure number = 1 */
    } = 1;                          /* version number = 1 */
    } = 0x31234567;                     /* program number = 0x31234567 */

client.c ファイル:

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <rpc/rpc.h>    /* standard RPC include file */
    #include "prog.h"       /* this file is generated by rpcgen */
    #include "math.h"
    main(int argc, char *argv[])
    {
        CLIENT *cl;         /* RPC handle */
        char *server;
char *word;
int *lresult;      /* return value from bin_date_1() */

if (argc != 3) {
    fprintf(stderr, "usage: %s hostname\n", argv[0]);
    exit(1);
}

server = argv[1];
word = argv[2];

printf("%s======\n", server);
printf("%s======\n", word);
/*
 * Create client handle
 */

if ((cl = clnt_create(server, PROGRAM_PROG, PROGRAM_VERS, "udp")) == NULL) {
    /*
     * can't establish connection with server
     */
     clnt_pcreateerror(server);
     exit(2);
}

/*
 * First call the remote procedure "bin_date".
 */
printf("123\n");
if ( (lresult = fun_1(&word, cl)) == NULL) {
    clnt_perror(cl, server);
    exit(3);
}
printf("456\n");
printf("time on host %s = %d and look, word: %s\n",server, *lresult, word);


clnt_destroy(cl);         /* done with the handle */
exit(0);

}

最後に serverproc.c ファイル:

    #include <stdio.h>
    #include <time.h>
    #include <string.h>
    #include <stdlib.h>
    #include <rpc/rpc.h>
    #include "prog.h"


    int *fun_1(char **p, CLIENT *cl)
    {
    int len;
    int t;
    t = strlen(*p);

    return &t;
    }

    int * fun_1_svc(char **p, struct svc_req *cl){
    CLIENT *client;
    return(fun_1(0, client));
    }

コードは正常にコンパイルされますが、実行するとハングするだけで、終了するには Ctrl-C を使用する必要があります。問題はおそらく serverproc.c ファイルにあります。私は何を間違っていますか?

4

2 に答える 2

0

次の理由が考えられます。

  • rpcbind または portmap がインストールされているかどうかを確認します
     sudo apt-get install rpcbind
  • インストールされている場合は、1 つのことだけを行います。: 次のようにrpcgenのみを使用して、クライアントとサーバーのコードを生成します。

     rpcgen prog.x -a 

サンプルのクライアントとサーバー コードが生成されます。

  • クライアントコードとサーバーコードに数行を追加するだけで、計算された長さを静的変数「結果」に設定します(すでに存在します)。
  • 試してみてください、きっとうまくいきます!!
于 2012-05-29T19:45:55.117 に答える
0

ハングは、サーバーがクライアント接続をリッスンしていることが原因です。ctrl+c を使用すると、サーバーが終了します。これは、serverproc.c に printf ステートメントを追加することで確認でき、クライアント接続が試行されるたびにサーバー コンソールに出力されるかどうかを確認できます。また、クライアントがハングしているように見える場合は、ポートマッパーを開始していない可能性があります。

ポートマッパーを起動するコマンド

OS コマンド Mac OS X

launchctl start com.apple.portmap 

Linux

/sbin/portmap

*BSD

/usr/sbin/rpcbind
于 2015-02-28T21:32:28.690 に答える