简体   繁体   English

使用C linux中的命名管道将数据从客户端发送到服务器

[英]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. 我正在尝试使用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. 我想将3件事(消息长度,消息类型和消息)发送到一个结构中,然后将指向该结构的指针发送到服务器。 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

//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. 参数m是正确的(它是结构),但是您应该将sizeof(m)更改为sizeof(*m) ,这样,您发送的字节数与结构中的字节数相同。 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. 您还需要在服务器上的相应read更改缓冲区大小。

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. 由于您仍然有一个msglen字段,因此您可以使用它来通知接收过程中接下来有多少个字节,并且它可以使用它来读取一些可变量。 So the reader would read the length field then get the rest of the message on a second read based on the length. 因此,读取器将读取长度字段,然后根据长度在第二次读取时获得其余消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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