title: How te send a string (char*) from server to client in onc-rpc
body: <p>I hope that someone can help me. I am making a program that sends a long variable to from the client to the server and the last must respond with a string. I would like to point out i am using onc-rpc framework(sunRPC if i am not mistaking).</p>
  
  <p>This is my current header => msg.x</p>
  
  <pre><code>//msg.x
  program MESSAGEPROG
  {
      version MESSAGEVERS
      {
          string FIBCALC(long) = 1;
       } = 1;
  } = 0x20000001;
  </code></pre>
  
  <p>My server stub must implement this function. I will not put all the code because it's a homework assigment.</p>
  
  <p>My server stub => server.c</p>
  
  <pre><code>#include &lt;rpc/rpc.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlb.h&gt;
  #include "msg.h"
  
  char ** fibcalc_1_svc(whatToUse, dummy)
      long *whatToUse;
      struct svc_req *dummy;
  {
      char whatToSend;
  
      whatToSend = (char **)malloc(sizeof(char*));
      *whatToSend = (char *)malloc(sizeof(char) * STRING_SIZE);
  
      //............
  
      return whatToSend;
  }
  </code></pre>
  
  <p>Needless to say the rest implementation works without the rpc. If i printf the string it works on a non rpc C file.</p>
  
  <pre><code>#include &lt;rpc/rpc.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlb.h&gt;
  #include "msg.h"
  
  int main(int argc, char *argv[])
  {
  CLIENT *cl;
  char **result;
  long *whatToSend, *test;
  FILE *fout, *fin;
  
  
  whatToSend = (long *)malloc(sizeof(long));
  result = (char **)malloc(sizeof(char*));
  *result = (char *)malloc(sizeof(char) * STRING_SIZE);
  
  if(argc != 3)
  {
      /* if arguments are not passed corectly
       * we print the following message and close with exit error
       */
      fprintf(stderr, "usage : ./%s [server ip] [fileIn]\n", argv[0]);
      exit(1);
  }
  
  cl = clnt_create(argv[1], 
                   MESSAGEPROG, 
                   MESSAGEVERS, 
                   "tcp");
  if(cl == NULL)
  {
      /* if no connection to server
       * we print the following message and close with exit error
       */
      clnt_pcreateerror(argv[1]);
      exit(1);
  }
  
  
  /* Sanity checks for file handle
   */
  fin = fopen(argv[2],"r");
  if (fin == NULL)
  {
      fprintf(stderr, "Input handle could not be opened!\n");
      exit(1);
  }   
  
  fout = fopen("out.txt", "w");
  if (fout == NULL)
  {
      fprintf(stderr, "Output handle could not be opened!\n");
      exit(1);
  }
  
  
  while(fscanf(fin, "%ld", whatToSend) != EOF)
  {
      memset(*result, 0, STRING_SIZE);
  
  
  
      result = fibcalc_1(whatToSend, cl);
  
      if(result == NULL)
      {
          /* Server did not respond
           */
          clnt_pcreateerror("localhost");
          exit(1);
      }
  
      printf("%s\n", *result);
  }
  
  /* Sanity checks for closing the handles
   */
  if(fclose(fin)) 
  {
      fprintf(stderr, "Input handle could not be closed!!\n");
      exit(1);
  }
  
  if(fclose(fout)) 
  {
      fprintf(stderr, "Output handle could not be closed!!\n");
      exit(1);
  }
  
  /* Free allocated memory
   */
  free(whatToSend);
  free(*result);
  free(result);
  
  exit(0);
  }
  </code></pre>
  
  <p>When i receive the server message i get seg fault. When i gdb, and step the client program at </p>
  
  <pre><code>result = fibcalc_1(whatToSend, cl); 
  </code></pre>
  
  <p>i get that the result address is 0x00</p>
  
  <p>When i change the result type to let's say an int or long or w/e the result comes out nicely and the program works.</p>
  
  <p>I would also like to point out that result is type char** because string is of type char * in onc-rpc and i came to realize that whatever variable the server function must return is the address of the return value. </p>
  
  <p>I hope i explained well enough my problem. My first thought was that in the server function the char whatToSend[20] should be of type char * that i should allocate, but then how do i deallocate it?</p>
  
  <p>Thank you in advance. </p>