2014-09-23 146 views
-3

我做了一个服务器/客户端应用程序,就像一个文件服务器。但是,当我尝试执行客户端时,它会退出并出现段违规行为。如果有人能够帮助我找到错误,我会将其粘贴到此处。由于RPC核心生成

client.c

#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); 
} 

Server.c

#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 */ 
+0

请问您可以添加头文件“ejer4.h”的代码 – 2014-09-23 18:15:22

+0

用eje4.h更新。谢谢 – user3420326 2014-09-23 19:53:58

+0

问题解决! – user3420326 2014-09-25 14:26:00

回答