[英]How to check if a message is empty
So I've been doing this for a whole day and didn't figure it out. 所以我已经整整整整一天都没有弄清楚。 I need to check if an empty message is passed to the receiver or server and then unlink queue.
我需要检查是否将空消息传递到接收方或服务器,然后取消链接队列。 This is what I have:
这就是我所拥有的:
while((c=getopt(argc, argv, ":q:"))!=-1){
switch(c){
case 'q':
q = 1;
Q = optarg;
break;
}
}
int oprimek_vrsta = -1;
char *msg = malloc(maxmsg_len + 1);
if(q != 0){
oprimek_vrsta = mq_open(Q, O_RDWR|O_CREAT|O_EXCL, 0660, &lastnosti_vrste);
if(oprimek_vrsta == -1){
perror("error creating queue");
return -1;
}
if(mq_getattr(oprimek_vrsta, &lastnosti_vrste) == -1){
perror("error reading attributes");
return -1;
}
while(loop){
memset(msg, 0, 4096);
munmap(msg, 4096);
msg_len = mq_receive(oprimek_vrsta, msg, maxmsg_len, &priority);
if(msg_len == -1){
perror("error reading message");
loop = 0;
free(msg);
mq_close(oprimek_vrsta);
mq_unlink(Q);
return -1;
}else{
write(1, msg, strlen(msg));
}
}
}
From mq_receive manual: 从mq_receive手册中:
mq_receive() removes the oldest message with the highest priority
from the message queue ... If the queue is empty, then, by default,
mq_receive() blocks until a message becomes available,
or the call is interrupted by a signal handler.
If the O_NONBLOCK flag is enabled for the message
queue description, then the call instead fails immediately with the error EAGAIN.
RETURN VALUE: On success, mq_receive() and mq_timedreceive() return the
number of bytes in the received message; on error, -1 is returned,
with errno set to indicate the error.
the return value of mq_receive is not what you're looking for. mq_receive的返回值不是您想要的。 You need to check whether your received message length is 0.
您需要检查收到的消息长度是否为0。
I also highly reccomend you to read this helpful tutorial about POSIX message queues. 我也强烈建议您阅读此有关POSIX消息队列的有用教程。
Here's a working example: 这是一个工作示例:
#include <stdio.h>
#include <mqueue.h>
#include <stdlib.h>
#include <string.h> /* memset */
#include <unistd.h> /* close */
#include <sys/mman.h>
#define QUEUE_NAME "/test"
#define MAX_MSG_LEN 100
int main(int argc, char **argv)
{
int q;
char* Q;
char c;
int loop = 1;
int msg_len;
struct mq_attr lastnosti_vrste;
lastnosti_vrste.mq_flags = 0;
lastnosti_vrste.mq_maxmsg = 10;
lastnosti_vrste.mq_msgsize = 100;
lastnosti_vrste.mq_curmsgs = 0;
while((c=getopt(argc, argv, "q:")) != -1)
{
switch(c)
{
case 'q':
mq_unlink(Q);
q = 1;
Q = QUEUE_NAME;
break;
}
}
int oprimek_vrsta = -1;
char *msg = malloc(MAX_MSG_LEN + 1);
if(q != 0)
{
oprimek_vrsta = mq_open(Q, O_RDWR|O_CREAT|O_EXCL, 0660, &lastnosti_vrste);
if(oprimek_vrsta == -1){
perror("error creating queue");
return -1;
}
if(mq_getattr(oprimek_vrsta, &lastnosti_vrste) == -1){
perror("error reading attributes");
return -1;
}
while(loop)
{
memset(msg, 0, 4096);
munmap(msg, 4096);
msg_len = mq_timedreceive(oprimek_vrsta, msg, MAX_MSG_LEN, 0);
if(strlen(msg) == 0)
{
perror("error reading message");
loop = 0;
mq_close(oprimek_vrsta);
mq_unlink(Q);
return -1;
}
else
{
write(1, msg, strlen(msg));
}
}
free(msg);
}
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.