当前位置:首页 » 编程语言 » C语言消息队发布订阅模式
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

C语言消息队发布订阅模式

发布时间: 2022-06-22 05:06:20

Ⅰ 用objective-c语言实现一个消息中心(NSnotificationcenter)功能

对象之间进行通信最基本的方式就是消息传递,在Cocoa中提供Notification Center机制来完成这一任务。其主要作用就是负责在任意两个对象之间进行通信。使用方法很简单,如下几个步骤即可:

假设A与B之间进行通信,B来触发事件,A接受该事件,并作出响应。
1) A编写自定义的消息响应函数update
2) A向消息中心注册,[NSNotificationCenter defaultCenter] addObserver: self selector:@selector(update) name:@"update" object:nil]
3) B触发事件[[NSNotificationCenter defaultCenter] postNotificationName:@"update" object:nil]

每一个进程都有一个默认的NSNotificationCenter,可以通过类方法defaultCenter获取该消息中心的实例。消息中心可以处理同一进程中不同对象之间的消息。如果要在同一台机器上进行进程间的通信,需要使用。

消息中心以同步的方式将消息分发到所有的观察者中,换言之,直到所有的观察者都收到消息并处理完毕以后,控制权才会回到调用者的手里。如果需要异步的处理消息,需要使用通知队列NSNotificationQueue。

在多线程程序中,通知会被分发到每一个发起消息的线程中,这可能与观察者注册时所在的线程已经不是同一线程。

实例:
@implementation TestClass

- (void) dealloc
{
// If you don't remove yourself as an observer, the Notification Center
// will continue to try and send notification objects to the deallocated
// object.
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}

- (id) init
{
self = [super init];
if (!self) return nil;

// Add this instance of TestClass as an observer of the TestNotification.
// We tell the notification center to inform us of "TestNotification"
// notifications using the receiveTestNotification: selector. By
// specifying object:nil, we tell the notification center that we are not
// interested in who posted the notification. If you provided an actual
// object rather than nil, the notification center will only notify you
// when the notification was posted by that particular object.

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receiveTestNotification:)
name:@"TestNotification"
object:nil];

return self;
}

- (void) receiveTestNotification:(NSNotification *) notification
{
// [notification name] should always be @"TestNotification"
// unless you use this method for observation of other notifications
// as well.

if ([[notification name] isEqualToString:@"TestNotification"])
NSLog (@"Successfully received the test notification!");
}

@end

Ⅱ C语言中,队列是什么意思,有什么用途

队列是一种特殊的线性表。

队列一种可以实现“先进先出”的存储结构,即“一端入,一端出”,队首(front)出队,队尾(rear)入队,若front指向队首,则rear指向队尾最后一个有效元素的下一个元素;若rear指向队尾,则front指向队首第一个有效元素的下一个元素。

队列特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表。进行插入操作的端称为队尾,进行删除操作的端称为队头。

(2)C语言消息队发布订阅模式扩展阅读

循环队列各个参数的含义

1、队列初始化front和rear的值都是零,初始化时队列就是空的。

2、队列非空front代表队列的第一个元素rear代表了最后一个有效元素的下一个元素。

3、队列空front和rear的值相等,但是不一定是零。

Ⅲ c语言:Windows Message Queue

消息队列是Windows系统的基本基础。 对于每一个过程中,系统维护一个消息队列。 如果事情发生在这个过程中,如鼠标单击,文字的变化,系统将添加一条消息到队列中。 同时,这一进程将做如何从根据消息队列的优先级值,如果它不是空的一个循环。 请注意以下优先级值意味着更高的优先权。 在这个问题中,你被要求模拟消息队列的消息队列将消息发送到和获得信息的。输入只有一个输入的测试为例。 每一行是一个命令,“GET”或“认沽”,这意味着获得的消息,或将邮件。 如果该命令是“把”外,还有一个字符串是指消息的名称和两个整数表示参数和优先其次。 最多将有60000命令。 请注意,一个消息可以出现两次或更多,如果两个消息具有相同的优先级,一来首先将首先处理。(即相同的优先级的FIFO。)进程的最终文件。每个输出为“ get“命令,该命令从输出的名字和在同一行参数消息队列中获取。 如果有任何消息队列中,输出“空队列!”。 有没有命令输出为“把”。样品输入获得放置msg1 10 10 4 5将msg2获得获得获得样本输出空队列!msg2 10 msg1 10空队列!

Ⅳ 关于c语言中消息队列发送的结构体

消息队列正常定义的type类型就是long,你看看是不是你定义的有问题。

#include<sys/types.h>

#include<sys/ipc.h>

#include<sys/msg.h>

#include<stdio.h>

struct msgbuf

{

long type;//类型

char buf[1024];

};

int main()

{

int msgid;

msgid=msgget(0x1000,IPC_CREAT | 0777);

struct msgbuf mb={1,"hello world"};

int ret;

ret=msgsnd(msgid,&mb,sizeof(struct msgbuf)-sizeof(long),0);

//这里的长度不包括类型的大小

}

Ⅳ c语言报刊订阅管理系统

如此不尊重劳动成果的明显是要沉的

Ⅵ 杂志订阅管理的c语言

#include<stdio.h>
#include "string.h"
#include "stdlib.h"
#include "conio.h"
#define max 20
struct magazine
{
char name[11];
char sex[6];
char addr[11];
char phone_number[12];
char unit_price[5];
char amount[4];
char time_limit[11];
}
order[max];void save(int n);
int load_record();
void search();
void printf_n(int n);
void printf_one(int i);
void input(int i);
void statistic();
void add();
void menu();void main()
{
FILE *fp;
fp=fopen("record.txt","w");/*在C:\Documents and Settings\Administrator里建立一个"record.txt"记事本*/
fclose(fp);
menu();
}void menu()
{
// void clrscr();
int w,n;
do
{
//clrscr();
printf("\n");
printf("\t\t* * * * * * * * * * * * * * * * * * * * * * * * *\n");
printf("\t\t \n");
printf("\t\t \n");
printf("\t\t 欢迎来到杂志订阅系统! \n");
printf("\t\t \n");
printf("\t\t \n");
printf("\t\t* * * * * * * * * * * * * * * * * * * * * * * * *\n");
printf("\n\n\t\t *********************************************\n\n");
printf("\t\t 1:add_new_subscriber\n");
printf("\t\t 2:browse\n");
printf("\t\t 3:statistic\n");
printf("\t\t 4:exit\n\n");
printf("\t\t *********************************************\n\n");
printf(" Choice your number(1-4):[ ]\b\b");
scanf("%d",&n);
if(n<1||n>4) w=1;
else w=0;
}
while (w==1);
/***************选择功能****************/
switch(n)
{
case 1:add();break; /*增加新订户记录*/
case 2:search();break; /*查找过期的订户记录并删除*/
case 3:statistic();break; /*统计*/
case 4:break; /*退出*/
}
}
/*********************添加模块************************/
void add()
{
int t,i,m;
system("cls");
t=load_record();
printf("您需要添加多少用户的信息?\n");
printf("输入号码:[ ]\b\b\b");
scanf("%d",&m);
for(i=0;i<m;i++)
{
printf("\n输入 %dth subscriber record.\n",i+1);
input(t+i); /*调用输入函数*/
}
save(t+m); /*调用保存函数 保存t+m组数据*/
system("cls") ; /*******清屏*********/
menu(); /*返回登陆界面*/
}
void input(int i)
{
printf("请输入:\n姓名 性别 地址 电话 杂志单价 数量 订阅期限\n");
scanf("%s%s%s%s%s%s%s",&order[i].name,&order[i].sex,&order[i].addr,&order[i].phone_number,&order[i].unit_price,&order[i].amount,&order[i].time_limit);
}
/**************************统计模块****************************/
void statistic()
{
int t;
t=load_record();
printf("订阅者的数量[%d]\b\b\b",t);
printf_n(t);
printf("\n\n\n按任意键返回...\n\n");
getch();
menu();
}void printf_one(int i) /*显示一个仪器记录的函数*/
{
printf("%-10s %-5s %-10s %-11s %-4s %-3s %-10s\n\n",order[i].name,order[i].sex,order[i].addr,order[i].phone_number,order[i].unit_price,order[i].amount,&order[i].time_limit);
}void printf_n(int n) /*显示n个仪器记录的函数*/
{
int j;
system("cls"); /**********清屏************/
printf("姓名 性别 地址 电话号码 价格 数量 时间期限\n\n");
for(j=0;j<n;j++)
{
if((j+1)%10==0) /*控制显示,每屏显示10行*/
{
printf("\n\n按任意键继续 ...");
getch();
puts("\n\n");
}
printf_one(j); /*调用显示一个仪器记录的函数*/
}
}
/***************查询模块****************/
void search()
{
FILE *fp;
char date[11],str[2];
int t,i,sum=0;
t=load_record();
system("cls");
printf("请输入日期:[ ]\b\b\b\b\b\b\b\b\b\b\b");
scanf("%s",date);
printf("\n");
if((fp=fopen("record.txt","w"))==NULL)/*以只写方式打开*/
{
printf("\n无法打开文件\n");
exit(0);
}
system("cls");
printf("\n");
for(i=0;i<t;i++)
{
if(strcmp(order[i].time_limit,date)<0)
{
sum++;

printf("\n");
printf_one(i); /**********把符合条件的记录显示出来*********/
printf("\n\n记录已过期!");
printf("\n\n你想要删除此条信息吗?('Y'or'N'):[ ]\b\b");
scanf("%s",str);;
if(strcmp(str,"Y")==0||strcmp(str,"y")==0)
continue;
}
fwrite(&order[i],sizeof(struct magazine),1,fp);
}
fclose(fp);
if(sum==0)
{
printf("***************************************************\n\n");
printf("很遗憾! 无法找到您想要的条件\n\n");
printf("***************************************************\n\n");
}
printf("\n\n\n按任意键返回前一步...\n");
getch();
menu();
}/****************************公共函数************************************/
int load_record()/***加载仪器信息并计算记录仪器个数的函数***/
{
FILE *fp;
int i;
if((fp=fopen("record.txt","r"))==NULL)/*以只读方式打开*/
{
printf("\n无法打开文件\n");
exit(0);
}
for(i=0;!feof(fp);i++)/*从文件中读入数据并保存在结构体数组中*/
fread(&order[i],sizeof(struct magazine),1,fp);
fclose(fp);
return(i-1);
}void save(int n)/*n表示保存n组数据,m表示保存在哪个文件夹里*/
{
FILE *fp;
int i;
fp=fopen("record.txt","w");/*以只写方式打开*/
if(fp==NULL)
{
printf("\n无法打开文件\n");
exit(0);
}
for(i=0;i<n;i++)
{
fwrite(&order[i],sizeof(struct magazine),1,fp);

}
fclose(fp);
}

Ⅶ 关于mosquitto怎么用c语言实现消息的订阅和发送(mqtt)

1.目标:测试Mosquitto使用MQTT协议发消息的相关性能指标,包含发送速度,并发负载能力,资源占用,消息到达率。
2.MQTT协议简介:
1).建立长连接。客户端发起请求和服务端建立长连接,建立成功后,服务端会返回ACK(CONNACK)
2).客户端订阅:客户端发起订阅,订阅成功后,服务端会返回ACK(SUBACK)
3).发消息:发布者会给服务端发消息,服务端在把消息给合适的客户端。
Qos=0(服务质量):客户端消息收到后,不会发出ACK给服务端(PUBACK)。
Qos =1:服务端会发ACK给发布者,客户端收到消息后会发ACK给服务端。
4).取消订阅:客户端发起取消订阅,服务端返回ACK(UNSUBACK)
5)Pingreq&Pingresp:客户端和服务端会保持心跳。
3.存在问题:
1. 如何模拟出40W的用户
2. 如何长连接,订阅,发消息,取消订阅,Pingreq行为如何实现。
4. python开源库 Mosquitto.py,解决所有问题
1. 模拟40W用户
a)可以使用虚拟机和Mosquitto.py实现,具体为:一般一台虚拟机最多是6W+的模拟数据(需要修改句柄数,我使用5W),方法是Client_id可以简单的做出5W个来,然后调用Mosquitto里面的connect方法,建立长连接。准备8台虚拟机就可以实现40W客户端的模拟
2.行为的模拟
a)订阅:Mosquitto.subscribe / 发消息:Mosquitto.publish / 取消订阅:Mosquitto.unsubscribe。 简单一句话 Mosquitto库实现所有的行为.
5. 指标:发送速度,到达率,并发负载,资源占用。
a. 发送速度:服务端日志记录,分析解决
b. 到达率: 1.客户端记录下收到消息,分析计算。2.计算服务端收到的PUBACK(客户端收到消息返回的ACK),进行计算
c. 并发负载:5W 用户不断增加,注意观察服务端的负载情况。
e.资源占用:其实主要是cpu/mem/带宽:cpu多核的话需要观察top命令下的_id字段, mem可以观察free -h命令的剩余mem, 带宽可以使用mpstat进行观察
6. 可以遇见问题:
a. 模拟客户端的虚拟机需要修改句柄数才能支持5W的客户端模拟数量
b. 要先吃透MQTT协议的流程,吃透了进行测试会非常的方便
c. Clear session,设置为true则不为客户端保留休息,设置为false保留消息。其实就是客户端离线后在连接上可以收到之前推出的消息。

Ⅷ C语言课程设计——杂志订阅管理系统

这个不是很难吧~我以前做过一个课程设计是通讯录管理系统

Ⅸ 用C语言,向一个指定的的窗口发送固定的信息,怎么实现啊 希望能提供源代码参考。希望大家不吝赐教

先取得窗口的句柄,如果是前台窗口,可以用 GetForegroundwindow获取!
如果知道窗口名等,也可以用FindWindow获取窗口句柄!

然后用SendMessage发送消息即可