1

我有一个客户端服务器程序,客户端在 PIPE 上为服务器编写命令。从服务器读取命令时,它只读取命令的第一个字符并引发错误。谁能帮我这个?

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <sys/wait.h>
#include <mqueue.h>
#include <sys/stat.h>
#include "Functions.h"

#define MSGBUFFER_SIZE 50000

pid_t serverPid;
pid_t clientPid;

typedef struct msgbuf {

int messageLength;
int messageType;
char messageText[MSGBUFFER_SIZE];

} Message_buf;

int writePIPE(int fd, Message_buf *inputMessage){

printf("\n In write pipe message length :%d",inputMessage->messageLength);
printf("\n In write pipe message Data :%s",inputMessage->messageText);
ssize_t n=write(fd,inputMessage,inputMessage->messageLength);
printf("\n Size :%d", n);
return n;
}

ssize_t readPIPE(int fd, Message_buf *outputMessage)
{
ssize_t len;
ssize_t n;
if((n=read(fd,outputMessage,sizeof(outputMessage)))==0)
{
    printf("\n Error");
    return 0;
}
if((len=outputMessage->messageLength)>0)
{
    printf("\n Length ---->:%d",len);
    if((n=read(fd,outputMessage->messageText,strlen(outputMessage->messageText)))!=len)
        printf("\n ERRRRROR expected %d got %d",len,n);
}
//printf("\n In Read PIPE: %s",outputMessage->messageText);
return len;
}
void Server(int readfd,int writefd)
{
Message_buf server_MessageBuf;

ssize_t length;
if((length=readPIPE(readfd,&server_MessageBuf))==0)
{
    printf("\n End of file while reading pathname");
}
//server_MessageBuf.messageText[length]='\0';

printf("\n LENGTH :%d",server_MessageBuf.messageLength);
printf("\n Printing in server: %s\n",server_MessageBuf.messageText);

}
void Client(int readfd,int writefd)
{

char inputFileName[MAX_SIZE];
char inputOperation[MAX_SIZE];
char *cCommandInput = NULL;
char *fileOperation = NULL;
char *operation = (char *) malloc(MAX_SIZE);

int commandValidateStatus = 0;
int commandInterpretationStatus=0;
Message_buf client_MessageBuf;
for(;;)
{
    while(1)
    {
        cCommandInput = acceptInput();
        fileOperation = (char *) malloc(sizeof(cCommandInput));
        strcpy(fileOperation,cCommandInput);

        /**Function call to determine operation read/delete/exit/invalid choice and filename*****/
        commandInterpretationStatus = interpretCommand(cCommandInput,
                inputOperation, inputFileName);

        operation = inputOperation;

        /**Function call to validate the input command******/
        commandValidateStatus = validateCommand(
                commandInterpretationStatus, inputOperation, inputFileName);

        if(commandValidateStatus==-1)
        {
            printf("\n Invalid Operation");
        }

        /*Exit command entered***/
        if (commandValidateStatus == 1)
        {
            /*Code to clear resources */
            kill(serverPid,SIGKILL);
            kill(clientPid,SIGKILL);
            exit(0);
        }
        /***Read or Delete****/
        if (commandValidateStatus == 2 || commandValidateStatus == 3)
        {
            printf("\n Read or Delete\n");

            strcpy(client_MessageBuf.messageText,fileOperation);
            client_MessageBuf.messageLength=strlen(fileOperation);
            client_MessageBuf.messageType=1;
            if((writePIPE(writefd,&client_MessageBuf))<0)
            {
                printf("\n Error writing on client side ");
            }

            //read(readfd,*client_MessageBuf,sizeof(client_MessageBuf));
            //printf("\n Reding server responsed");
            //printf("%s",client_MessageBuf.messageText);
        }
    }
}

}

int main()
{
int pipe1[2],pipe2[2];
pipe(pipe1);
pipe(pipe2);

pid_t pid;
pid=fork();
serverPid=pid;

if(pid==0)
{
    /*Call Server*/
    close(pipe1[1]);
    close(pipe2[0]);
    Server(pipe1[0], pipe2[1]);   
}
else
{
    close(pipe1[0]);
    close(pipe2[1]);
    Client(pipe2[0],pipe1[1]);      
}
return 0;
}
4

1 回答 1

0

看起来代码写入和读取struct msgbuf不正确:

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,&client_MessageBuf))<0)

// ....

int writePIPE(int fd, Message_buf *inputMessage){
    printf("\n In write pipe message length :%d",inputMessage->messageLength);
    printf("\n In write pipe message Data :%s",inputMessage->messageText);
    ssize_t n=write(fd,inputMessage,inputMessage->messageLength);
    printf("\n Size :%d", n);
    return n;
}

上面写的部分struct msgbuf只写messageLength结构的第一个字节,不包括messageLengthmessageType成员的长度,即它截断对象。

阅读时:

ssize_t readPIPE(int fd, Message_buf *outputMessage)
{
    // ...
    if((n=read(fd,outputMessage,sizeof(outputMessage)))==0)

它只读取sizeof(outputMessage)字节,即指针的大小,而不是对象。即使您通过将其更改为sizeof(*outputMessage)that 来修复它也不够,因为它会期望读取完整的对象,而写入部分会截断对象。

修复它的一个好的开始是将消息分成两部分:标头和有效负载。header 是一个固定大小的结构,例如:

typedef struct {
    int messageType;
    int payloadLength;
} MessageHeader;

有效载荷是头部后面的可变长度部分。这样,它将首先将整个标头对象写入管道,然后payloadLength是有效负载字节。读取时,它将首先再次读取整个标头,然后准确读取payloadLength字节。

另请注意,read()andwrite()调用可能读取或写入的次数少于请求的次数,因此需要通过保持读取或写入来明确处理这种情况,直到处理完确切的字节数。

于 2012-05-07T12:32:31.950 回答