简体   繁体   中英

sending data from client to server using named pipe in C linux

I am trying to send data form client to server using named pipes in C linux. I want to send 3 things(message length, message type and message) which I have put in a structure and sending pointer to that struct to the server. But only message length is getting sent but other two things are not.

here is my code

//client.c
//header files
typedef struct msgclient
{
    int msglen;
    int msgtype;
    char cp[100];
}M1;
int main()
{
    M1 *m;
    int rdfd, wrfd, numread;
    int ans;
    m=malloc(sizeof(M1));
    do
    {
            printf("\nEnter message type\n1. YOU WANT TO ENTER DATA\n2. AUTOMATIC DATA\n3. EXIT\nENTER YOUR CHOICE\t");
            scanf("%d",&m->msgtype);

            switch(m->msgtype)
            {
                    case 1: //memset(buf,'\0',(strlen(buf)+1));
                            m->msgtype=1;
                            printf("\nEnter data\t");
                            getchar();
                            scanf("%s",&m->cp);
                            m->cp[strlen(m->cp)]='\0';
                            m->msglen=strlen(m->cp);

                            break;
                    case 2: m->msgtype=2;
                            strncpy(m->cp,"I am HERE!!",strlen("I am HERE!!"));
                            m->cp[strlen("I am HERE!!")]='\0';
                            m->msglen=strlen(m->cp);
                            break;
                    case 3: exit(0);
                    default:printf("\nINVALID CHOICE\nTRY AGAIN\n1. YES \n2. NO\n");
                            scanf("%d",&ans);
                            continue;

            }
            wrfd = open(NP1, O_WRONLY);
            numread=write(wrfd, m, sizeof(m));
            printf("\nno. of bytes SENT TO the SERVER = %d",numread);
            printf("\n%s",m->cp);
            printf("\n%d",m->msgtype);
            printf("\nSEND AGAIN\n1. YES \n2. NO\t");
            getchar();
            scanf("%d",&ans);
    }while(ans==1);

    return 0;
}

Now server.c

//server.c
//header files
typedef struct msgclient
{
    int msglen;
    int msgtype;
    char cp[100];
}M1;
int main()
{
    M1 *m;
    int rdfd, wrfd, ret_val, numread;
    int ans=1;
    m=malloc(sizeof(M1));
    ret_val = mkfifo(NP1,0666);

    if ((ret_val == -1) && (errno != EEXIST))
    {
            perror("Error creating the named pipe");
            exit (1);
    }
    ret_val = mkfifo(NP2, 0666);
    if ((ret_val == -1) && (errno != EEXIST))
    {
            perror("Error creating the named pipe");
            exit (1);
    }
    while(ans!=0)
    {
            rdfd = open(NP1, O_RDONLY);
            if((numread = read(rdfd, m, sizeof(m)))==0)
                    return 0;
            printf("\nno. of bytes read from the client = %d\n\n",numread);
            fputs(m->cp,stdout);
            //printf("message is %s",m->cp);
            printf("\nLENGTH = %d\nTYPE = %d\nMESSAGE IS %s \nRECEIVED BY SERVER\n",m->msglen,m->msgtype,m->cp);
            printf("\nSENT TO CLIENT\n");
    }
    return 0;
}

Please help me in this. Thanks :)

The conceptual problem is you are "sending pointer to that struct to the server". The client and server have different memory spaces, and they cannot share pointers. You need to send the struct itself. Fortunately, this is almost what you are doing in your program.

In concrete terms the trouble is with:

M1 *m;
m=malloc(sizeof(M1));
.
.
.
numread=write(wrfd, m, sizeof(m));

The argument m is correct (it is the struct), but you should change sizeof(m) to sizeof(*m) , that way you send as many bytes as are in the struct. Your current code sends only as many bytes as a pointer would have. You also need to change the buffer-size in the corresponding read on the server.

You can't send the pointer to the structure because it has no meaning in the receiving process. Since the two process don't share memory what would it point to in the receiving process?

You need to send the entire structure sans pointers. Since you have a msglen field anyway, you can use this to inform the receiving process of how many bytes follow and the it can use this to read some variable amount. So the reader would read the length field then get the rest of the message on a second read based on the length.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM