daily pastebin goal
66%
SHARE
TWEET

Untitled

a guest Sep 14th, 2018 45 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
  1. Error writing and reading a structure from PIPE
  2. #include <stdio.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. #include <errno.h>
  6. #include <stdlib.h>
  7. #include <sys/ipc.h>
  8. #include <sys/msg.h>
  9. #include <string.h>
  10. #include <sys/wait.h>
  11. #include <mqueue.h>
  12. #include <sys/stat.h>
  13. #include "Functions.h"
  14.  
  15. #define MSGBUFFER_SIZE 50000
  16.  
  17. pid_t serverPid;
  18. pid_t clientPid;
  19.  
  20. typedef struct msgbuf {
  21.  
  22. int messageLength;
  23. int messageType;
  24. char messageText[MSGBUFFER_SIZE];
  25.  
  26. } Message_buf;
  27.  
  28. int writePIPE(int fd, Message_buf *inputMessage){
  29.  
  30. printf("n In write pipe message length :%d",inputMessage->messageLength);
  31. printf("n In write pipe message Data :%s",inputMessage->messageText);
  32. ssize_t n=write(fd,inputMessage,inputMessage->messageLength);
  33. printf("n Size :%d", n);
  34. return n;
  35. }
  36.  
  37. ssize_t readPIPE(int fd, Message_buf *outputMessage)
  38. {
  39. ssize_t len;
  40. ssize_t n;
  41. if((n=read(fd,outputMessage,sizeof(outputMessage)))==0)
  42. {
  43.     printf("n Error");
  44.     return 0;
  45. }
  46. if((len=outputMessage->messageLength)>0)
  47. {
  48.     printf("n Length ---->:%d",len);
  49.     if((n=read(fd,outputMessage->messageText,strlen(outputMessage->messageText)))!=len)
  50.         printf("n ERRRRROR expected %d got %d",len,n);
  51. }
  52. //printf("n In Read PIPE: %s",outputMessage->messageText);
  53. return len;
  54. }
  55. void Server(int readfd,int writefd)
  56. {
  57. Message_buf server_MessageBuf;
  58.  
  59. ssize_t length;
  60. if((length=readPIPE(readfd,&server_MessageBuf))==0)
  61. {
  62.     printf("n End of file while reading pathname");
  63. }
  64. //server_MessageBuf.messageText[length]='';
  65.  
  66. printf("n LENGTH :%d",server_MessageBuf.messageLength);
  67. printf("n Printing in server: %sn",server_MessageBuf.messageText);
  68.  
  69. }
  70. void Client(int readfd,int writefd)
  71. {
  72.  
  73. char inputFileName[MAX_SIZE];
  74. char inputOperation[MAX_SIZE];
  75. char *cCommandInput = NULL;
  76. char *fileOperation = NULL;
  77. char *operation = (char *) malloc(MAX_SIZE);
  78.  
  79. int commandValidateStatus = 0;
  80. int commandInterpretationStatus=0;
  81. Message_buf client_MessageBuf;
  82. for(;;)
  83. {
  84.     while(1)
  85.     {
  86.         cCommandInput = acceptInput();
  87.         fileOperation = (char *) malloc(sizeof(cCommandInput));
  88.         strcpy(fileOperation,cCommandInput);
  89.  
  90.         /**Function call to determine operation read/delete/exit/invalid choice and filename*****/
  91.         commandInterpretationStatus = interpretCommand(cCommandInput,
  92.                 inputOperation, inputFileName);
  93.  
  94.         operation = inputOperation;
  95.  
  96.         /**Function call to validate the input command******/
  97.         commandValidateStatus = validateCommand(
  98.                 commandInterpretationStatus, inputOperation, inputFileName);
  99.  
  100.         if(commandValidateStatus==-1)
  101.         {
  102.             printf("n Invalid Operation");
  103.         }
  104.  
  105.         /*Exit command entered***/
  106.         if (commandValidateStatus == 1)
  107.         {
  108.             /*Code to clear resources */
  109.             kill(serverPid,SIGKILL);
  110.             kill(clientPid,SIGKILL);
  111.             exit(0);
  112.         }
  113.         /***Read or Delete****/
  114.         if (commandValidateStatus == 2 || commandValidateStatus == 3)
  115.         {
  116.             printf("n Read or Deleten");
  117.  
  118.             strcpy(client_MessageBuf.messageText,fileOperation);
  119.             client_MessageBuf.messageLength=strlen(fileOperation);
  120.             client_MessageBuf.messageType=1;
  121.             if((writePIPE(writefd,&client_MessageBuf))<0)
  122.             {
  123.                 printf("n Error writing on client side ");
  124.             }
  125.  
  126.             //read(readfd,*client_MessageBuf,sizeof(client_MessageBuf));
  127.             //printf("n Reding server responsed");
  128.             //printf("%s",client_MessageBuf.messageText);
  129.         }
  130.     }
  131. }
  132.  
  133. }
  134.  
  135. int main()
  136. {
  137. int pipe1[2],pipe2[2];
  138. pipe(pipe1);
  139. pipe(pipe2);
  140.  
  141. pid_t pid;
  142. pid=fork();
  143. serverPid=pid;
  144.  
  145. if(pid==0)
  146. {
  147.     /*Call Server*/
  148.     close(pipe1[1]);
  149.     close(pipe2[0]);
  150.     Server(pipe1[0], pipe2[1]);  
  151. }
  152. else
  153. {
  154.     close(pipe1[0]);
  155.     close(pipe2[1]);
  156.     Client(pipe2[0],pipe1[1]);      
  157. }
  158. return 0;
  159. }
  160.    
  161. typedef struct msgbuf {
  162.     int messageLength;
  163.     int messageType;
  164.     char messageText[MSGBUFFER_SIZE];
  165. } Message_buf;
  166.  
  167. // ...
  168.  
  169. strcpy(client_MessageBuf.messageText,fileOperation);
  170. client_MessageBuf.messageLength=strlen(fileOperation);
  171. client_MessageBuf.messageType=1;
  172. if((writePIPE(writefd,&client_MessageBuf))<0)
  173.  
  174. // ....
  175.  
  176. int writePIPE(int fd, Message_buf *inputMessage){
  177.     printf("n In write pipe message length :%d",inputMessage->messageLength);
  178.     printf("n In write pipe message Data :%s",inputMessage->messageText);
  179.     ssize_t n=write(fd,inputMessage,inputMessage->messageLength);
  180.     printf("n Size :%d", n);
  181.     return n;
  182. }
  183.    
  184. ssize_t readPIPE(int fd, Message_buf *outputMessage)
  185. {
  186.     // ...
  187.     if((n=read(fd,outputMessage,sizeof(outputMessage)))==0)
  188.    
  189. typedef struct {
  190.     int messageType;
  191.     int payloadLength;
  192. } MessageHeader;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top