c - Using IPC Sys V -
i have server , client , want send file client server, using sys v ipc. problem sending of file, server creating file stops @ msgrcv_3 invalid argument (last while in server). maybe have idea.
snippet of server:
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); } }
snippet of client:
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 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; }
the structur:
#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
Comments
Post a Comment