我有一个服务器和一个客户端,并希望通过使用Sys V IPC将文件从客户端发送到服务器。我的问题是发送文件,服务器正在创建文件但是停止在msgrcv_3无效的参数(最后在服务器中执行)。也许有人有个主意。
服务器片段:
while(1){
int ausgabe=0;
/* Messageq */
if ((msqid = msgget(server_key, IPC_CREAT|IPC_EXCL|0666)) < 0){
perror("msgget_1");
exit(EXIT_FAILURE);
}
/* Message Empfangen */
if((msreturn = msgrcv(msqid, (void*)&name, sizeof(struct name),0,0))<0){
perror("msgrcv_1");
exit(EXIT_FAILURE);
}
result.exists = FileExists(name.file_name);
printf("%d\n", result.exists);
/* Message Versenden*/
if((msreturn = msgsnd(msqid, (void*)&result, sizeof(struct result),0))<0){
perror("msgsnd_1");
exit(EXIT_FAILURE);
}
if(result.exists == 42){
if((msreturn = msgrcv(msqid, (void*)&data, sizeof(struct data),0,0))<0){
perror("msgrcv_2");
exit(EXIT_FAILURE);
}
creatfile = open(data.file_name, O_WRONLY | O_CREAT| O_EXCL , 0);
if(creatfile == -1){
perror("creratfile");
exit(EXIT_FAILURE);
}
do{
if(msgrcv(msqid, (void*)&msg_body, sizeof(struct msg_body),0,0)<0){
perror("msgrcv_3");
exit(EXIT_FAILURE);
}
buff_blub = write(creatfile, &msg_body.buffer, msg_body.bufsize);
if (buff_blub < 0){
perror("write");
exit(EXIT_FAILURE);
}
ausgabe = ausgabe+msg_body.bufsize;
}
while(ausgabe != data.datasize);
chmod(data.file_name, 0644);
}
}
客户端代码:
strncpy(name.file_name, argv[2], sizeof(name.file_name) - 1);
/* Message Versenden*/
if((msreturn = msgsnd(msqid, (void*)&name, sizeof(struct name),0))<0){
perror("msgsnd");
exit(EXIT_FAILURE);
}
/* Message Empfangen */
if((msreturn = msgrcv(msqid, (void*)&result, sizeof(struct result),0,0))<0){
perror("msgrcv");
exit(EXIT_FAILURE);
}
if(result.exists == 1){
puts("File already exists");
exit(EXIT_FAILURE);
}
else if(result.exists == 42){
file_read = open(argv[1], O_RDONLY);
if (file_read > 0){
if (fstat(file_read, &sb)==-1){
perror("fstat");
return EXIT_FAILURE;
}
strncpy(data.file_name, argv[2], sizeof(data.file_name) - 1);
data.datasize = sb.st_size;
data.c_pid = getpid();
if((msreturn = msgsnd(msqid, (void*)&data, sizeof(struct data),0))<0){
perror("msgsnd");
exit(EXIT_FAILURE);
}
while((file_buffer = read (file_read, &msg_body.buffer, BUF_SIZE)) > 0){
msg_body.bufsize = file_buffer;
msg_body.prio = 2;
if((msreturn = msgsnd(msqid, (void*)&msg_body, sizeof(struct msg_body),0))<0){
perror("msgsnd");
exit(EXIT_FAILURE);
}
}
close(file_read);
}
}
return 1;
}
结构:
#include <unistd.h>
#if !defined(__MSGBSP_H)
#define __MSGBSP_H
#define BUF_SIZE 1024
struct data {
char file_name[255];
ssize_t datasize;
long c_pid;
};
struct name {
char file_name[255];
};
struct result {
int exists;
};
struct header {
int size;
};
struct msg_body {
long prio;
ssize_t bufsize;
char buffer[BUF_SIZE];
};
#endif