gpt4 book ai didi

c - 基本的unix服务器-客户端IPC(消息队列)问题

转载 作者:行者123 更新时间:2023-11-30 15:56:49 25 4
gpt4 key购买 nike

我正在解决一个问题,这意味着一个基本的正在运行的服务器客户端(之前完成过一个)。问题是我运行服务器而不是客户端。我的 msg Que 是在两个客户端都将我的字符作为输入发送的情况下创建的,我收到确认打印,但我的服务器 msgrcv 没有响应。

SC

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#include <errno.h>

#include "struc.h"


int main(){
int qt;
struct msg m;

qt = msgget(1271,IPC_CREAT|IPC_EXCL|0600);
if(qt < 0){ perror("Error MSGGET()\n");}
printf("msg queue created!\n");

if(msgrcv(qt,&m,sizeof(struct msg),0,0)<0){
perror("Msg recive error");
}
printf("msg recived!\n");


msgctl(qt,IPC_RMID,NULL);
return 0;
}

抄送

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#include <errno.h>

#include "struc.h"

int main(){
int qt;
struct msg m;
qt = msgget(1271,0);
if(qt < 0){ perror("~~~ Error MSGGET()\n");}
printf("msg created!\n");

printf("Enter one char: !\n");
scanf("%c",&m.c);

msgsnd(qt, &m,sizeof(struct msg),0);
printf("msg sent!\n");
return 0;
}

struc.h

struct msg{
long mtype;
// matrix M;
char c;
};

(通过创建 3 个文件,您可以自己进行测试。欢迎任何想法,也许我错过了一些东西)

最佳答案

您应该执行此操作以验证发送不会失败。

 if(msgsnd(qt, &m,sizeof(struct msg),0)) < 0) {
perror("Msg send error");
}

您还应该注意 msgsnd 的文档:

The msgp argument is a pointer to caller-defined structure of the following general form:       struct msgbuf {           long mtype;       /* message type, must be > 0 */           char mtext[1];    /* message data */        };

That is, you have to set the mtype in the message you send to be > 0. Currently it is uninitialized, so you should do:

m.mtype = 1;
if(msgsnd(qt, &m,sizeof(struct msg),0)) < 0) {
perror("Msg send error");
}

关于c - 基本的unix服务器-客户端IPC(消息队列)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10921535/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com