Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It looks like the code writes and reads <code>struct msgbuf</code> incorrectly:</p> <pre><code>typedef struct msgbuf { int messageLength; int messageType; char messageText[MSGBUFFER_SIZE]; } Message_buf; // ... strcpy(client_MessageBuf.messageText,fileOperation); client_MessageBuf.messageLength=strlen(fileOperation); client_MessageBuf.messageType=1; if((writePIPE(writefd,&amp;client_MessageBuf))&lt;0) // .... int writePIPE(int fd, Message_buf *inputMessage){ printf("\n In write pipe message length :%d",inputMessage-&gt;messageLength); printf("\n In write pipe message Data :%s",inputMessage-&gt;messageText); ssize_t n=write(fd,inputMessage,inputMessage-&gt;messageLength); printf("\n Size :%d", n); return n; } </code></pre> <p>The above pieces that write <code>struct msgbuf</code> only write the first <code>messageLength</code> bytes of the structure which doesn't include the length of <code>messageLength</code> and <code>messageType</code> members, i.e. it truncates the object.</p> <p>When reading:</p> <pre><code>ssize_t readPIPE(int fd, Message_buf *outputMessage) { // ... if((n=read(fd,outputMessage,sizeof(outputMessage)))==0) </code></pre> <p>It reads only <code>sizeof(outputMessage)</code> bytes, which is the size of the pointer, not the object. Even if you fix it by changing it to <code>sizeof(*outputMessage)</code> that won't do enough, since that would expect to read the complete object whereas the writing piece truncates the object.</p> <p>A good start to fix it would be to split the message into two parts: header and payload. The header is a structure of a fixed size, e.g.:</p> <pre><code>typedef struct { int messageType; int payloadLength; } MessageHeader; </code></pre> <p>The payload is a variable-length part following the header. This way it would first write the entire header object into the pipe followed by <code>payloadLength</code> bytes of payload. When reading it would first read again the entire header and then read exactly <code>payloadLength</code> bytes.</p> <p>Also note, that <code>read()</code> and <code>write()</code> calls may read or write less then asked, so that case needs to be explicitly handled by keeping reading or writing until the exact number of bytes has been processed.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload