2010-01-17 14 views
9

मैं दूरस्थ सर्वर पर बाइनरी फ़ाइलों को स्थानांतरित करना चाहता हूं। मैं अपने कोड के लिए सन/ओएनसी आरपीसी (लिनक्स पर आरपीसीजेन) का उपयोग कर रहा हूं। मैं सी का उपयोग कर रहा हूं। मैंने सर्वर और क्लाइंट के लिए कोड लिखा है और यह टेक्स्ट फाइलों के लिए काम करता है, लेकिन जब मैं बाइनरी फाइलों को स्थानांतरित करने का प्रयास करता हूं तो यह कहता है कि फ़ाइल स्थानांतरण के बाद दूषित हो गई है। मैं चरित्र सरणी या एक्सडीआर तारों में डेटा भाग संग्रहित कर रहा हूं। मुझे लगता है कि मेरे साथ एक चरित्र सरणी के रूप में डेटा भंडारण में कुछ समस्या है। क्या कोई मुझे बता सकता है कि समस्या क्या है? क्या कोई मेरी सहायता कर सकता है?सूर्य आरपीसी: बाइनरी फाइलों को स्थानांतरित करना

मैं संदर्भ के लिए यहां अपने कोड स्निपेट संलग्न कर रहा हूं यदि कोई ऐसा देखना चाहता है कि मैं क्या कर रहा हूं।

मेरे आईडीएल:

const MAXLEN = 1024; 

/* 
* Type for storing path 
*/ 
typedef string filename<MAXLEN>; 

/* 
* Structure for sending request. Expects the path of the file 
* and the byte number at which to start reading the file from 
*/ 
struct request { 
    filename name; 
    int start; 
}; 

/* 
* Type that represents the structute for request 
*/ 
typedef struct request request; 

/* 
* Type for storing a chunk of the file that is being 
* sent from the server to the client in the current 
* remote procedure call 
*/ 
typedef string filechunk<MAXLEN>; 

/* 
* Response sent by the server to the client as a response 
* to remote procedure call, containing the filechunk for 
* the current call and number of bytes actually read 
*/ 
struct chunkreceive { 
    filechunk data; 
    int bytes; 
}; 

/* 
* Type that represents the structure for file's chunks 
* to be received from the server 
*/ 
typedef struct chunkreceive chunkreceive; 

/* 
* File data sent by the server from client to store 
* it on the server along with the filename and the 
* number of bytes in the data 
*/ 
struct chunksend { 
    filename name; 
    filechunk data; 
    int bytes; 
}; 

/* 
* Type that represents the structure for file's chunks 
* to be sent to the server 
*/ 
typedef struct chunksend chunksend; 

/* 
* union for returning from remote procedure call, returns 
* the proper chunkdata response if everything worked fine 
* or will return the error number if an error occured 
*/ 
union readfile_res switch (int errno) { 
    case 0: 
     chunkreceive chunk; 
    default: 
     void; 
}; 

/* 
* Remote procedure defined in the Interface Definition Language 
* of SUN RPC, contains PROGRAM and VERSION name definitions and 
* the remote procedure signature 
*/ 
program FTPPROG { 
    version FTPVER { 
     readfile_res retrieve_file(request *) = 1; 
     int send_file(chunksend *) = 2; 
    } = 1; 
} = 0x20000011; 

मेरे सर्वर:

#include <rpc/rpc.h> 
#include <stdio.h> 
#include "ftp.h" 

extern __thread int errno; 

readfile_res* retrieve_file_1_svc(request *req, struct svc_req *rqstp) 
{ 
    FILE *file; 
    char data[1024]; 
    int bytes; 
    static readfile_res res; 

    file = fopen(req->name, "rb"); 
    if (file == NULL) { 
     res.errno = errno; 
     return (&res); 
    } 

    fseek (file, req->start, SEEK_SET); 
    bytes = fread(data, 1, 1024, file); 

    res.readfile_res_u.chunk.data = data; 
    res.readfile_res_u.chunk.bytes = bytes; 

    /* 
    * Return the result 
    */ 
    res.errno = 0; 
    fclose(file); 
    return (&res); 
} 

int* send_file_1_svc(chunksend *rec, struct svc_req *rqstp) 
{ 
    FILE *file; 
    int write_bytes; 
    static int result; 

    file = fopen(rec->name, "a"); 
    if (file == NULL) { 
     result = errno; 
     return &result; 
    } 

    write_bytes = fwrite(rec->data, 1, rec->bytes, file); 
    fclose(file); 

    result = 0; 
    return &result; 
} 

मेरा ग्राहक:

#include <rpc/rpc.h> 
#include <stdio.h> 
#include <string.h> 
#include "ftp.h" 

extern __thread int errno; 

int get_file(char *host, char *name) 
{ 
    CLIENT *clnt; 
    int total_bytes = 0, write_bytes; 
    readfile_res *result; 
    request req; 
    FILE *file; 

    req.name = name; 
    req.start = 0; 

    /* 
    * Create client handle used for calling FTPPROG on 
    * the server designated on the command line. Use 
    * the tcp protocol when contacting the server. 
    */ 
    clnt = clnt_create(host, FTPPROG, FTPVER, "tcp"); 
    if (clnt == NULL) { 
     /* 
     * Couldn't establish connection with server. 
     * Print error message and stop. 
     */ 
     clnt_pcreateerror(host); 
     exit(1); 
    } 

    file = fopen(name, "wb"); 

    /* 
    * Call the remote procedure readdir on the server 
    */ 
    while (1) { 
     req.start = total_bytes; 
     result = retrieve_file_1(&req, clnt); 
     if (result == NULL) { 
      /* 
      * An RPC error occurred while calling the server. 
      * Print error message and stop. 
      */ 
      clnt_perror(clnt, host); 
      exit(1); 
     } 

     /* 
     * Okay, we successfully called the remote procedure. 
     */ 
     if (result->errno != 0) { 
      /* 
      * A remote system error occurred. 
      * Print error message and stop. 
      */ 
      errno = result->errno; 
      perror(name); 
      exit(1); 
     } 

     /* 
     * Successfully got a chunk of the file. 
     * Write into our local file. 
     */ 
     write_bytes = fwrite(result->readfile_res_u.chunk.data, 1, result->readfile_res_u.chunk.bytes, file); 
     total_bytes += result->readfile_res_u.chunk.bytes; 
     if (result->readfile_res_u.chunk.bytes < MAXLEN) 
      break; 
    } 

    fclose(file); 

    return 0; 
} 

int put_file(char *host, char *name) 
{ 
    CLIENT *clnt; 
    char data[1024]; 
    int total_bytes = 0, read_bytes; 
    int *result; 
    chunksend chunk; 
    FILE *file; 

    /* 
    * Create client handle used for calling FTPPROG on 
    * the server designated on the command line. Use 
    * the tcp protocol when contacting the server. 
    */ 
    clnt = clnt_create(host, FTPPROG, FTPVER, "tcp"); 
    if (clnt == NULL) { 
     /* 
     * Couldn't establish connection with server. 
     * Print error message and stop. 
     */ 
     clnt_pcreateerror(host); 
     exit(1); 
    } 

    file = fopen(name, "r"); 

    chunk.name = name; 

    /* 
    * Call the remote procedure readdir on the server 
    */ 
    while (1) { 
     read_bytes = fread(data, 1, MAXLEN, file); 
     total_bytes += read_bytes; 

     chunk.data = data; 
     chunk.bytes = read_bytes; 
     result = send_file_1(&chunk, clnt); 

     if (result == NULL) { 
      /* 
      * An RPC error occurred while calling the server. 
      * Print error message and stop. 
      */ 
      clnt_perror(clnt, host); 
      exit(1); 
     } 

     /* 
     * Okay, we successfully called the remote procedure. 
     */ 
     if (*result != 0) { 
      /* 
      * A remote system error occurred. 
      * Print error message and stop. 
      */ 
      errno = *result; 
      perror(name); 
      exit(1); 
     } 

     /* 
     * Successfully got a chunk of the file. 
     * Write into our local file. 
     */ 
     if (read_bytes < MAXLEN) 
      break; 
    } 

    fclose(file); 

    return 0; 
} 

int read_command(char *host) 
{ 
    char command[MAXLEN], filepath[MAXLEN]; 

    printf("> "); 
    fflush(stdin); 
    scanf("%s %s", command, filepath); 

    if (strcmp(command, "get") == 0) { 
     return get_file(host, filepath); 
    } else if(strcmp(command, "put") == 0){ 
     return put_file(host, filepath); 
    } else if(strcmp(command, "exit") == 0){ 
     exit(0); 
    } else { 
     return -1; 
    } 
} 

int main(int argc, char *argv[]) 
{ 
    int result; 

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

    while(TRUE) { 
     result = read_command(argv[1]); 
    } 

    return 0; 
} 

उत्तर

4

एक्सडीआर तारों को निरस्त कर दिया गया है। बाइनरी डेटा को स्थानांतरित करने के लिए आपको एक अलग डेटा प्रकार का उपयोग करने की आवश्यकता है - शायद 'बाइट सरणी'। उदाहरण के लिए, सूर्य पर यह document देखें।

+0

धन्यवाद। इस पोस्ट को देखने से पहले मुझे यह काम मिल गया था। वैसे भी जाने का रास्ता है, अब मैं पूर्णांक सरणी का उपयोग कर रहा हूँ! –

0

से पहले और स्थानांतरण के बाद फ़ाइलों की तुलना करें, जहां समस्या है कि आपको बता देंगे । इसके लिए आप hexdiff का उपयोग कर सकते हैं।

+0

ऐसा लगता है कि लूप के प्रत्येक पुनरावृत्ति के दौरान फ़ाइल का केवल एक छोटा हिस्सा आ रहा है। सर्वर की तरफ जब यह फ़ाइल को पढ़ता है, तो आकार सही होते हैं, लेकिन जब क्लाइंट साइड पर प्राप्त होता है, तो यह सब गड़बड़ हो जाता है। केवल आधा डेटा सही है! –

3

थोड़ा देर से मुझे लगता है कि यह आपकी समस्या का समाधान है: जस्टी फ़ाइल के एक हिस्से को मनमाने ढंग से बाइट्स की एक निश्चित लंबाई सरणी में संग्रहीत करने के लिए प्रकार को बदलता है। तो अपने आईडीएल में, घोषणा के बजाय "typedef स्ट्रिंग < filechunk MAXLEN >;" अपने अपारदर्शी डेटा इस्तेमाल कर सकते हैं: "अपारदर्शी filechunk [MAXLEN] typedef;" (तथ्य की बात है, यह सिर्फ चार की एक निश्चित सरणी है)

पीएस: उस तरह का डेटा (निश्चित सरणी) आपको फ़ाइल से पढ़ने या लिखने के लिए एक बफर के रूप में एक चर का उपयोग करने से रोकता है। उदाहरण के लिए, समारोह में * retrieve_file_1_svc * अपने सर्वर से, बयानों

*bytes = fread(data, 1, 1024, file); 
res.readfile_res_u.chunk.data = data;* 

, Madhusudan.CS ही "समाधान" पूर्णांकों का उपयोग कर देंगे बस भविष्य में संदर्भ के

*bytes = fread(res.readfile_res_u.chunk.data, 1, 1024, file);* 
1

को परिवर्तित करना विभिन्न endianness के साथ मशीनों का उपयोग करते समय आप सभी तरह के मजेदार। आरपीसी को उस मामले में पूर्णांक का अनुवाद करना चाहिए, अपनी स्ट्रिंग या बाइनरी डेटा को मक्का करना चाहिए।

सही समाधान 'अपारदर्शी' एक्सडीआर डेटा प्रकार का उपयोग कर रहा है। यह बाइट्स की मात्रा के लिए _len हस्ताक्षरित int के साथ एक संरचना बनाएगा, और एक _var सूचक जो आप अपने डेटा को इंगित कर सकते हैं।

संबंधित मुद्दे