发送带有消息队列的数组
我一直在尝试发送带有消息队列的数组。它可以与普通字符串一起使用,但我不知道如何使用数组进行操作。到目前为止,这是我的代码。我需要分开作家和接收者吗?我还需要再发送两次数组,但是在第一次发送之前,我无法启动它。 感谢您的帮助。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <time.h>
#include <sys/msg.h>
#define maxArr 100
//create buffer
struct msgbuffer{
long msgtype;
char msgtext [100];
int arr[maxArr];
}msg;
int main(int argc , char ** argv){
//needed variables
int returncode_msgget;
int returncode_msgrcv;
int i;
//create message queue
returncode_msgget = msgget(1234, IPC_CREAT |0600);
//generating random numbers
srand(time(NULL));
for (i = 0; i < 10; i++) {
msg.arr[i] = rand() %101;
printf("%d,", msg.arr[i]);
}
//sending message
msg.msgtype = 1;
strcpy(msg.msgtext,"test");
//checking if message was sent
if (msgsnd(returncode_msgget, &msg, sizeof(msg.msgtext), 0) == -1){
printf("message not sent\n");
perror("msgsnd()");
}
//buffer for message receiver
wait(NULL);
struct msgbuffer{
long mtype;
char mtext [100];
} msg;
// reaching message queue
//checking if message queue is reachable
msg.mtype = 1;
if ((returncode_msgget = msgget(1234, IPC_CREAT| 0600)) < 0) {
printf("could not reach Message queue\n");
perror("msgget()");
}
//checking if message was received
returncode_msgrcv = msgrcv(returncode_msgget, &msg, sizeof(msg.mtext), msg.mtype, MSG_NOERROR|IPC_NOWAIT);
if (returncode_msgrcv == -1) {
printf("message not received\n");
perror("msgrcv()");
//if message received, notification
}else {
printf ("Diese Nachricht wurde aus der Warteschlange gelesen : %s \n" , msg . mtext );
printf ("Die empfangene Nachricht ist %i Zeichen lang .\n" , returncode_msgrcv );
}
printf("test");
}
i have been trying to send an array with message queues in c. It works with a normal string, but I don´t know how to do it with an array. this is my code so far with the string attempt. Do I need to seperate the writer and receiver? I also need to send the array two more times, but I can´t start before it sends the first time.
thanks for any help.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <time.h>
#include <sys/msg.h>
#define maxArr 100
//create buffer
struct msgbuffer{
long msgtype;
char msgtext [100];
int arr[maxArr];
}msg;
int main(int argc , char ** argv){
//needed variables
int returncode_msgget;
int returncode_msgrcv;
int i;
//create message queue
returncode_msgget = msgget(1234, IPC_CREAT |0600);
//generating random numbers
srand(time(NULL));
for (i = 0; i < 10; i++) {
msg.arr[i] = rand() %101;
printf("%d,", msg.arr[i]);
}
//sending message
msg.msgtype = 1;
strcpy(msg.msgtext,"test");
//checking if message was sent
if (msgsnd(returncode_msgget, &msg, sizeof(msg.msgtext), 0) == -1){
printf("message not sent\n");
perror("msgsnd()");
}
//buffer for message receiver
wait(NULL);
struct msgbuffer{
long mtype;
char mtext [100];
} msg;
// reaching message queue
//checking if message queue is reachable
msg.mtype = 1;
if ((returncode_msgget = msgget(1234, IPC_CREAT| 0600)) < 0) {
printf("could not reach Message queue\n");
perror("msgget()");
}
//checking if message was received
returncode_msgrcv = msgrcv(returncode_msgget, &msg, sizeof(msg.mtext), msg.mtype, MSG_NOERROR|IPC_NOWAIT);
if (returncode_msgrcv == -1) {
printf("message not received\n");
perror("msgrcv()");
//if message received, notification
}else {
printf ("Diese Nachricht wurde aus der Warteschlange gelesen : %s \n" , msg . mtext );
printf ("Die empfangene Nachricht ist %i Zeichen lang .\n" , returncode_msgrcv );
}
printf("test");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我尝试发送一个数组的尝试,我在此处的另一篇文章中看到了该数组:
this was my attempt for sending an array which I saw on another post here: