#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <errno.h>
typedef struct {
long mtype;
char mtext[1024]; // 假设消息文本的最大长度为1024字节
} message;
int main() {
key_t key = ftok("some_key_file", 'a');
int msqid = msgget(key, 0666 | IPC_CREAT);
if (msqid == -1) {
perror("msgget");
return 1;
}
message msg;
ssize_t msglen = 0;
while ((msglen = msgrcv(msqid, &msg, sizeof(msg.mtext), 1, MSG_NOERROR)) == -1) {
if (errno == E2BIG) {
fprintf(stderr, "Buffer too small, resizing...\n");
// 这里可以调整缓冲区大小,例如,可以增加mtext的大小
// 然后重新尝试接收消息
} else {
perror("msgrcv");
break;
}
}
if (msglen > 0) {
printf("Received message of type %ld with size %zd\n", msg.mtype, msglen);
// 处理接收到的消息
}
// 清理资源
if (msgctl(msqid, IPC_RMID, NULL) == -1) {
perror("msgctl");
}
return 0;
}
标签:E2BIG,msqid,msgrcv,kimi,key,msg,include,msglen
From: https://www.cnblogs.com/qqiwei/p/18308829