繁体   English   中英

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

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

我正在尝试使用C linux中的命名管道将数据表单客户端发送到服务器。 我想将3件事(消息长度,消息类型和消息)发送到一个结构中,然后将指向该结构的指针发送到服务器。 但是,只有消息长度正在发送,而其他两件事却没有。

这是我的代码

//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;
}

现在是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;
}

请帮助我。 谢谢 :)

概念上的问题是您正在“将指向该结构的指针发送到服务器”。 客户端和服务器具有不同的内存空间,并且它们不能共享指针。 您需要发送结构本身。 幸运的是,这几乎是您在程序中所做的。

具体来说,问题在于:

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

参数m是正确的(它是结构),但是您应该将sizeof(m)更改为sizeof(*m) ,这样,您发送的字节数与结构中的字节数相同。 您当前的代码仅发送与指针一样多的字节。 您还需要在服务器上的相应read更改缓冲区大小。

您不能将指针发送到该结构,因为它在接收过程中没有任何意义。 由于这两个进程不共享内存,因此在接收进程中将指向什么?

您需要发送不带指针的整个结构。 由于您仍然有一个msglen字段,因此您可以使用它来通知接收过程中接下来有多少个字节,并且它可以使用它来读取一些可变量。 因此,读取器将读取长度字段,然后根据长度在第二次读取时获得其余消息。

暂无
暂无

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

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