Ⅰ 用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發送消息即可