c语言订阅发布,杂志订阅系统C语言程序设计

本文目录一览:

用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获取该消息中心的实例。消息中心可以处理同一进程中不同对象之间的消息。如果要在同一台机器上进行进程间的通信,需要使用NSDistributedNotificationCenter。

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

关于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)PingreqPingresp:客户端和服务端会保持心跳。

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语言

#includestdio.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(n1||n4) 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;im;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;jn;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;it;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;in;i++)

{

fwrite(order[i],sizeof(struct magazine),1,fp);

}

fclose(fp);

}

原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/291103.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2024-12-24 13:14
下一篇 2024-12-24 13:14

相关推荐

  • Deepin系统分区设置教程

    本教程将会详细介绍Deepin系统如何进行分区设置,分享多种方式让您了解如何规划您的硬盘。 一、分区的基本知识 在进行Deepin系统分区设置之前,我们需要了解一些基本分区概念。 …

    编程 2025-04-29
  • AES加密解密算法的C语言实现

    AES(Advanced Encryption Standard)是一种对称加密算法,可用于对数据进行加密和解密。在本篇文章中,我们将介绍C语言中如何实现AES算法,并对实现过程进…

    编程 2025-04-29
  • 学习Python对学习C语言有帮助吗?

    Python和C语言是两种非常受欢迎的编程语言,在程序开发中都扮演着非常重要的角色。那么,学习Python对学习C语言有帮助吗?答案是肯定的。在本文中,我们将从多个角度探讨Pyth…

    编程 2025-04-29
  • 如何在树莓派上安装Windows 7系统?

    随着树莓派的普及,许多用户想在树莓派上安装Windows 7操作系统。 一、准备工作 在开始之前,需要准备以下材料: 1.树莓派4B一台; 2.一张8GB以上的SD卡; 3.下载并…

    编程 2025-04-29
  • Java任务下发回滚系统的设计与实现

    本文将介绍一个Java任务下发回滚系统的设计与实现。该系统可以用于执行复杂的任务,包括可回滚的任务,及时恢复任务失败前的状态。系统使用Java语言进行开发,可以支持多种类型的任务。…

    编程 2025-04-29
  • Python被称为胶水语言

    Python作为一种跨平台的解释性高级语言,最大的特点是被称为”胶水语言”。 一、简单易学 Python的语法简单易学,更加人性化,这使得它成为了初学者的入…

    编程 2025-04-29
  • OpenJudge答案1.6的C语言实现

    本文将从多个方面详细阐述OpenJudge答案1.6在C语言中的实现方法,帮助初学者更好地学习和理解。 一、需求概述 OpenJudge答案1.6的要求是,输入两个整数a和b,输出…

    编程 2025-04-29
  • Python按位运算符和C语言

    本文将从多个方面详细阐述Python按位运算符和C语言的相关内容,并给出相应的代码示例。 一、概述 Python是一种动态的、面向对象的编程语言,其按位运算符是用于按位操作的运算符…

    编程 2025-04-29
  • 分销系统开发搭建

    本文主要介绍如何搭建一套完整的分销系统,从需求分析、技术选型、开发、部署等方面进行说明。 一、需求分析 在进行分销系统的开发之前,我们首先需要对系统进行需求分析。一般来说,分销系统…

    编程 2025-04-29
  • 学堂云Python语言程序设计答案

    学堂云Python语言程序设计是一门重要的计算机专业课程。它涵盖了Python语言及其应用,包括基础语法、函数、文件处理、数据结构、图形界面和网络编程等内容。在学习中,我们经常会需…

    编程 2025-04-29

发表回复

登录后才能评论