c - 发送 POSIX 消息队列时接收额外字符

标签 c posix message-queue

我几乎完成了一个简单的 fork 程序,其中一个进程使用 mq_send 和 mq_receive 向其父进程发送一组字符,并输出反转的字符串。但是,由于某种原因,当我有时输入要发送的字符串时,程序的接收端会在其中添加字符。更重要的是,这看起来是随机的,因为再次发送相同的字符串将给出正确的输出。

这是完整的代码,以及后面的示例输出。

#include  <stdio.h> /* printf */
#include  <sys/types.h> /* pid_t */
#include <unistd.h> /* get_pid */
#include <stdlib.h>     /* exit, EXIT_FAILURE */
#include <sys/wait.h>   /* wait */
#include <mqueue.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <string.h>

#define MAX_STR_LEN 500

void reverse(char s[]);
void clientFunction(const char *msgqname);
void serverFunction(const char *msgqname);
int main(void)
{
    //A
    char msgQName[] = "/queue";
    //B
    pid_t pid;
    pid = fork();
    if (pid == 0){      
        clientFunction(msgQName);
    }
    else {
        usleep(10L);
        serverFunction(msgQName);
    }
    return(0);

}
void clientFunction(const char *msgqname){
    //C

    mqd_t mqident = mq_open(msgqname, O_WRONLY|O_CREAT ,  0666, NULL);
    //D
    if (mqident == -1){
        printf("Error opening message queue. Exiting program. \n");
        exit(0);
    }
    char str[MAX_STR_LEN];  //Keep the string that has been read from input and written on the pipe
    memset(str,0,strlen(str));  
    while(1){

        printf("Enter an string:");
        gets(str);  //Reads the string from input
        printf("%i\n",strlen(str));
        unsigned len = (unsigned) strlen(str); //Finds the length of the string
        int sent = mq_send(mqident,str,len,0);
        if (sent == -1){
            printf("Error sending message. Exiting program. \n");
            exit (0);
        }
        usleep(10L);
        memset(str,0,strlen(str));
    }

}
void serverFunction(const char *msgqname){
    //F
    mqd_t mqident = mq_open (msgqname,O_RDONLY|O_CREAT , 0666, NULL);

    if (mqident == -1){
        printf("Error opening message queue. Exiting program. \n");
        printf( "Error : %s\n", strerror( errno ) );
        exit(0);
    }
    char str1[MAX_STR_LEN];
    memset(str1,0,strlen(str1));
    struct mq_attr attr;
    while(1){
        //G
        mq_getattr(mqident,&attr);
        //H
        ssize_t receive = mq_receive(mqident,str1,attr.mq_msgsize,0);
        if (receive == -1){
            printf("Error receiving message. Exiting program. \n");
            exit(0);
        }
        printf("%i, %i\n",strlen(str1), attr.mq_msgsize);
        reverse(str1);
        printf("What you wrote in reverse was:%s\n", str1); 
        memset(str1,0,strlen(str1));
    }

}
void reverse(char s[])
{
      int length = strlen(s) ;
      int c, i, j;

      for (i = 0, j = length - 1; i < j; i++, j--)
     {
     c = s[i];
     s[i] = s[j];
     s[j] = c;
      }
}

在终端中使用 gcc 并使用 -o 和 -lrt 进行编译时的示例输出

Enter an string:tester 1234567890     
17
21
What you wrote in reverse was:1��0987654321 retset
Enter an string:tester 1234567890
17
17
What you wrote in reverse was:0987654321 retset
Enter an string:

输出中的两个整数值是我用来检查长度的,第一个是发送字符串的 strlen,第二个是接收字符串的 strlen。

我确保在初始化和 printf 之后立即清空字符串,所以我不知道这些神秘字符来自哪里。

最佳答案

void serverfunction()void clientFunction()

替换memset(str1,0,strlen(str1))

memset(str1,0,sizeof(str1))

sizeof 将给出数组的总大小

现在您可能不会获得垃圾数据。

关于c - 发送 POSIX 消息队列时接收额外字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19167401/

相关文章:

c# - 我们怎样才能加快从 MSMQ 接收消息的速度?

objective-c - 如何在 NSLog() 中打印 unsigned char*

C:读入多个文件

c - 引发后续警报()

c - Posix C 管道延迟

sql-server - SQL Server Service Broker 的缺点

java - 我向 activemq 代理发送一条预定消息,但 Web 控制台中显示两条消息

c++ - fileno, errno : What does the "no" mean?(非母语英语)

c - 使两个结构指针相等

c - fget(c) - 在保存前一个字符的同时扫描下一个字符