您好,
会员登录 快速注册
退出 ( 条未读消息)
关于本站 意见反馈 首页

公告:小宅博客网可以开发票了,需要发票的,去群里找群主哈!!
全部文章分类
  • 人工智能 >

  • 编程语言 >

  • WPF系列 >

  • ASP.NET系列 >

  • Linux >

  • 数据库 >

  • 嵌入式 >

  • WEB技术 >

  • PLC系列 >

  • 微服务与框架 >

  • 小宅DIY >

  • 学习资料 >

OpenCv基础 ANN车牌识别 yolov5车牌识别 指针式仪表识别 ROS系列 YOLO Halcon Detectron2 昇腾AI ChatGPT在线体验 英伟达JETSON ChatGLM ChatTTS FunASR 地平线 ByteTrack 魔搭社区 LangChain
C C# C++ Python Java Go
WPF
ASP.NET小功能 GPS定位系统-MVC GPS定位系统-VUE ASP.NET WebRTC
Linux Linux内核 Shell MakeFile
MySql SqlServer Oracle
STM8 STM32 51单片机
VUE入门 HTML JavaScript CSS layui镜像网站 ElementUi中文官网 element-plus 图标
三菱 欧姆龙 西门子 施耐德 松下 台达
IOTSharp IOTGateway ABP FRAMEWORK Docker
亚克力音响 编程仙途:智驭万法
面试题与技巧 Python入门技能树 微软C#教程
首页 编程之美 工具下载 全国就业 流量地图 文心一言
C
1、99乘法表 2、指针5分钟教程 3、12个有趣的C语言问答 佛祖保佑,永无BUG Linux MQ接收与发送 C语言 TCP客户端
佛祖保佑,永无BUG
C语言 TCP客户端
激萌の小宅 小宅博客 C

文章作者:激萌の小宅

促销:¥0

价格:¥0

配送方式: 购买后立即生效(如购买异常,请联系站长)
付款之后一定要等待自动跳转结束,否则购买可能会失败
  • 0 天

    有效期

  • 0

    总销量

  • 0

    累计评价

Linux MQ接收与发送

参数说明:

// 消息队列的标志。目前,这个字段只用于设置或获取O_NONBLOCK标志,以指示消息队列是否应该以非阻塞模式打开
attr.mq_flags = 0;

// 队列中允许的最大消息数
attr.mq_maxmsg = 10;

// 每条消息的最大字节数
attr.mq_msgsize = MSG_SIZE;

// 当前在队列中的消息数(注意:这个字段在mq_open调用时应该被设置为0,因为它是只读属性,用于获取当前队列中的消息数量,而不是设置它)
attr.mq_curmsgs = 0;


代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mqueue.h>
#include <pthread.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>

#define MAX_SIZE 1024

// 发送线程函数
void* sender_thread(void* arg) {
    char msg_buf[] = "Hello, POSIX message queue!";
    struct mq_attr attr;
    attr.mq_flags = 0;
    attr.mq_maxmsg = 10;
    attr.mq_msgsize = MAX_SIZE;
    attr.mq_curmsgs = 0;

    // 创建消息队列
    mqd_t mq = mq_open("/queue_a", O_CREAT | O_RDWR, 0644, &attr);
    if (mq == (mqd_t)-1) {
        perror("a mq open");
        exit(1);
    }

    while (1) {
        if (mq_send(mq, msg_buf, strlen(msg_buf) + 1, 0) == -1) {
            perror("mq_send");
        }
        // printf("Message sent: %s\n", msg_buf);
        sleep(1);  // 等待接收线程处理或模拟发送间隔
    }

    // 清理资源(在实际应用中,应该在适当的时机关闭消息队列和线程)
    mq_close(mq);
    mq_unlink("/queue_a");
    return NULL;
}

// 接收线程函数
void* receiver_thread(void* arg) {
    struct mq_attr attr;
    attr.mq_flags = 0;
    attr.mq_maxmsg = 10;
    attr.mq_msgsize = MAX_SIZE;
    attr.mq_curmsgs = 0;

    char buffer[MAX_SIZE];
    ssize_t bytes_read;

    // 创建消息队列
    mqd_t mq = mq_open("/queue_b", O_CREAT | O_RDWR, 0644, &attr);
    if (mq == (mqd_t)-1) {
        perror("b mq open");
        exit(1);
    }

    while (1) {
        bytes_read = mq_receive(mq, buffer, MAX_SIZE, NULL);
        if (bytes_read != -1) {
            printf("Message received: %s\n", buffer);
        }
        usleep(100);
    }

    // 清理资源(在实际应用中,应该在适当的时机关闭消息队列和线程)
    mq_close(mq);
    mq_unlink("/queue_b");
    return NULL;
}

int main() {
    pthread_t sender_tid, receiver_tid;
    if (pthread_create(&sender_tid, NULL, sender_thread, NULL) != 0) {
        perror("pthread_create sender");
        exit(1);
    }
    if (pthread_create(&receiver_tid, NULL, receiver_thread, NULL) != 0) {
        perror("pthread_create receiver");
        exit(1);
    }
    pthread_join(sender_tid, NULL);
    pthread_join(receiver_tid, NULL);
    return 0;
}


编译命令

gcc -o e e.c -lrt -lpthread

佛祖保佑,永无BUG
C语言 TCP客户端

友情链接: CSDN激萌の小宅 95知识库 自考题库 罗分明个人网络博客 精益编程leanboot

小宅博客  www.bilibili996.com All Rights Reserved. 备案号: 闽ICP备2024034575号

网站经营许可证  福建省福州市 Copyright©2021-2025 版权所有

小宅博客
首页 智能家居 地图定位
公告:小宅博客网可以开发票了,需要发票的,去群里找群主哈!!

文章作者:激萌の小宅

促销:¥0

价格:¥0

配送方式: 购买后立即生效(如购买异常,请联系站长)
付款之后一定要等待自动跳转结束,否则购买可能会失败
  • 0 天

    有效期

  • 0

    总销量

  • 0

    累计评价

Linux MQ接收与发送

参数说明:

// 消息队列的标志。目前,这个字段只用于设置或获取O_NONBLOCK标志,以指示消息队列是否应该以非阻塞模式打开
attr.mq_flags = 0;

// 队列中允许的最大消息数
attr.mq_maxmsg = 10;

// 每条消息的最大字节数
attr.mq_msgsize = MSG_SIZE;

// 当前在队列中的消息数(注意:这个字段在mq_open调用时应该被设置为0,因为它是只读属性,用于获取当前队列中的消息数量,而不是设置它)
attr.mq_curmsgs = 0;


代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mqueue.h>
#include <pthread.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>

#define MAX_SIZE 1024

// 发送线程函数
void* sender_thread(void* arg) {
    char msg_buf[] = "Hello, POSIX message queue!";
    struct mq_attr attr;
    attr.mq_flags = 0;
    attr.mq_maxmsg = 10;
    attr.mq_msgsize = MAX_SIZE;
    attr.mq_curmsgs = 0;

    // 创建消息队列
    mqd_t mq = mq_open("/queue_a", O_CREAT | O_RDWR, 0644, &attr);
    if (mq == (mqd_t)-1) {
        perror("a mq open");
        exit(1);
    }

    while (1) {
        if (mq_send(mq, msg_buf, strlen(msg_buf) + 1, 0) == -1) {
            perror("mq_send");
        }
        // printf("Message sent: %s\n", msg_buf);
        sleep(1);  // 等待接收线程处理或模拟发送间隔
    }

    // 清理资源(在实际应用中,应该在适当的时机关闭消息队列和线程)
    mq_close(mq);
    mq_unlink("/queue_a");
    return NULL;
}

// 接收线程函数
void* receiver_thread(void* arg) {
    struct mq_attr attr;
    attr.mq_flags = 0;
    attr.mq_maxmsg = 10;
    attr.mq_msgsize = MAX_SIZE;
    attr.mq_curmsgs = 0;

    char buffer[MAX_SIZE];
    ssize_t bytes_read;

    // 创建消息队列
    mqd_t mq = mq_open("/queue_b", O_CREAT | O_RDWR, 0644, &attr);
    if (mq == (mqd_t)-1) {
        perror("b mq open");
        exit(1);
    }

    while (1) {
        bytes_read = mq_receive(mq, buffer, MAX_SIZE, NULL);
        if (bytes_read != -1) {
            printf("Message received: %s\n", buffer);
        }
        usleep(100);
    }

    // 清理资源(在实际应用中,应该在适当的时机关闭消息队列和线程)
    mq_close(mq);
    mq_unlink("/queue_b");
    return NULL;
}

int main() {
    pthread_t sender_tid, receiver_tid;
    if (pthread_create(&sender_tid, NULL, sender_thread, NULL) != 0) {
        perror("pthread_create sender");
        exit(1);
    }
    if (pthread_create(&receiver_tid, NULL, receiver_thread, NULL) != 0) {
        perror("pthread_create receiver");
        exit(1);
    }
    pthread_join(sender_tid, NULL);
    pthread_join(receiver_tid, NULL);
    return 0;
}


编译命令

gcc -o e e.c -lrt -lpthread