- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想将二进制文件传输到远程服务器。我为我的代码使用 SUN/ONC RPC(Linux 上的 rpcgen)。我正在使用 C。我已经为服务器和客户端编写了代码,它适用于文本文件,但是当我尝试传输二进制文件时,它说文件在传输后已损坏。我将数据 block 存储在字符数组或 XDR 字符串中。我认为我将数据存储为字符数组存在一些问题。有人可以告诉我问题是什么吗?有人可以帮帮我吗?
如果有人想看看我在做什么,我会在此处附上我的代码片段以供引用。
我的 IDL:
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;
}
最佳答案
XDR 字符串以 null 结尾。您需要使用不同的数据类型来传输二进制数据——可能是“字节数组”。参见,例如,这个 document在星期日。
关于c - 太阳 RPC : transferring binary files,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2082033/
版本 java :10.0.1 Tomcat :8.0.36Ubuntu:18.04(64 位)Eclipse:光子(64 位) 错误:当我运行 Tomcat 服务器时,我发现了以下错误。请不要告诉我
我想将二进制文件传输到远程服务器。我为我的代码使用 SUN/ONC RPC(Linux 上的 rpcgen)。我正在使用 C。我已经为服务器和客户端编写了代码,它适用于文本文件,但是当我尝试传输二进制
我想在 Java 中使用 BouncyCaSTLe 和 PKCS11 库解密一个 CMSEnvelopedData。一切顺利,直到我遇到这个问题: 我可以成功检索收件人信息: CMSEnveloped
我低于异常 sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.ce
我在一个大型 Java 项目中工作,它有大约 400 个类。突然报错 sun.awt.image.PNGImageDecoder$PNGException: crc corruption at
我正在使用 Liferay v6.2 CE 并且出现以下错误: 2015 2:14:34 ΜΜ org.apache.catalina.startup.ContextConfig getServle
我正在学习 Java、Spring MVC 和 Eclipse。我现在正在读一本书,并试图让第一个 Spring MVC 示例运行。当我从 Eclipse 运行它时,出现此错误: INFO: TLD
我已经使用此命令创建了 CSR 请求: openssl req -out certificatecsr.csr -new -newkey rsa:2048 -keyout certificatekey
我们是三个开发 JSF 项目的人,我们之前没有接触过 JSF。奇怪的是,从 SVN check out 相同的数据,其中一名团队成员每次使用 Tomcat 7.0.27 时都会收到 404 错误。 T
我在 Apache Tomcat 7 上有一个 Web 应用程序,我的 Web 应用程序上有不受信任的证书。我的 Web 应用程序必须与另一个使用 https 的 Web 应用程序通信。但是我总是得到
在我运行以下代码时出现错误: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX
我已经从一台服务器迁移到一台新服务器。我为我的移动应用程序使用 PHP 网络服务来访问数据库中的数据。我已经安装了 SSL 证书,web 服务在浏览器和 iphone 应用程序(使用 ASIHTTPR
为什么在 HashMap 上调用 containsKey 比 get 慢? 测试:http://ideone.com/QsWXF (>15% 的差异,在 sun-jdk-1.6.0.17 上运行) 最
我是一名优秀的程序员,十分优秀!