我做了一个服务器/客户端应用程序,就像文件服务器一样。但是当我尝试执行客户端时,它以段冲突退出。我将代码粘贴在这里,如果有人可以帮助我发现错误,我将不胜感激。谢谢

客户端

#include "ejer4.h"
#include <stdlib.h>

retR sread(CLIENT *clnt,char *fd,int pos,int count){
    paramsR p;
    retR *result;
    /* Gather everything into a single data structure to send to the            server */
    p.fd = (char *)malloc((strlen(fd)+1)*sizeof(char));
    p.fd = fd;
    p.pos = pos;
    p.count = count;

    /* Call the client stub created by rpcgen */
    result = serverread_1(&p,clnt);
    if (result==NULL) {
    fprintf(stderr,"Trouble calling remote procedure\n");
    exit(0);
  }
    return (*result);
}

int swrite(CLIENT *clnt,char *fd,char *buf,int count){
    paramsW p;
    int *result;

    /* Gather everything into a single data structure to send to the            server */
    p.fd = (char *)malloc((strlen(fd)+1)*sizeof(char));
    p.fd = fd;
    p.buf = (char *)malloc((count+1)*sizeof(char));
    p.buf = buf;
    p.count = count;

    /* Call the client stub created by rpcgen */
    result = serverwrite_1(&p,clnt);
    if (result==NULL) {
    fprintf(stderr,"Trouble calling remote procedure\n");
    exit(0);
  }
    return (*result);
}

int
main (int argc, char *argv[])
{
    retR res;
    paramsR p;
    CLIENT *clnt;
  char *buffer = (char*) malloc(7*sizeof(char));
  if (argc!=2) {
        printf("%s host",argv[0]);
    }


  /* Create a CLIENT data structure that reference the RPC
     procedure SIMP_PROG, version SIMP_VERSION running on the
     host specified by the 1st command line arg. */

  clnt = clnt_create(argv[1], ejer4_prog, ejer4_version, "tcp");

  /* Make sure the create worked */
  if (clnt == (CLIENT *) NULL) {
    clnt_pcreateerror(argv[1]);
    exit(1);
  }

  res = sread(clnt,"texto",0,6);

  printf("res = %s , count = %d \n",res.buf,res.count);
  return(0);
}


服务器

#include "ejer4.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>

retR *
serverread_1_svc(paramsR *argp, struct svc_req *rqstp)
{
    exit(0);
    static retR result;
    int ok=1;
    int seek;
    int f1 = open(argp->fd,O_RDONLY);
    if(f1 != -1){
        seek = lseek(f1,argp->pos,SEEK_SET);
        if(seek!=-1){
            result.buf = (char *)malloc(argp->count*sizeof(char));
            result.count = read(f1,result.buf,argp->count);
        }else{
            ok=0;
        }
        close(f1);
    }else{
        ok=0;
    }

    if(!ok){
        printf("Problema con el archivo\n");
        result.buf=NULL;
        result.count=0;
    }
    return (&result);
}

int *
serverwrite_1_svc(paramsW *argp, struct svc_req *rqstp)
{
    static int  result;
  int f1 = open(argp->fd,O_APPEND | O_CREAT);
    int ok = 1;
    if(f1 != -1){
        result = write(f1,argp->buf,argp->count);
        close(f1);
    }else{
        printf("Error al abrir archivo...");
        result = 0;
    }
    return (&result);
}


谢谢大家问候

注意:这是ejer4.h

#ifndef _EJER4_H_RPCGEN
#define _EJER4_H_RPCGEN

#include <rpc/rpc.h>


#ifdef __cplusplus
extern "C" {
#endif


struct paramsR {
    char fd[10];
    int pos;
    int count;
};
typedef struct paramsR paramsR;

struct retR {
    char buf[7];
    int count;
};
typedef struct retR retR;

struct paramsW {
    char fd[10];
    int count;
    char buf[7];
};
typedef struct paramsW paramsW;

#define ejer4_prog 0x20000001
#define ejer4_version 1

#if defined(__STDC__) || defined(__cplusplus)
#define ServerRead 1
extern  retR * serverread_1(paramsR *, CLIENT *);
extern  retR * serverread_1_svc(paramsR *, struct svc_req *);
#define ServerWrite 2
extern  int * serverwrite_1(paramsW *, CLIENT *);
extern  int * serverwrite_1_svc(paramsW *, struct svc_req *);
extern int ejer4_prog_1_freeresult (SVCXPRT *, xdrproc_t, caddr_t);

#else /* K&R C */
#define ServerRead 1
extern  retR * serverread_1();
extern  retR * serverread_1_svc();
#define ServerWrite 2
extern  int * serverwrite_1();
extern  int * serverwrite_1_svc();
extern int ejer4_prog_1_freeresult ();
#endif /* K&R C */

/* the xdr functions */

#if defined(__STDC__) || defined(__cplusplus)
extern  bool_t xdr_paramsR (XDR *, paramsR*);
extern  bool_t xdr_retR (XDR *, retR*);
extern  bool_t xdr_paramsW (XDR *, paramsW*);

#else /* K&R C */
extern bool_t xdr_paramsR ();
extern bool_t xdr_retR ();
extern bool_t xdr_paramsW ();

#endif /* K&R C */

#ifdef __cplusplus
}
#endif

#endif /* !_EJER4_H_RPCGEN */

最佳答案

问题解决了! .x文件出问题。

关于c - 生成RPC内核,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26000237/

10-11 16:44