C语言实现mq收发数据的函数
amqsget.c 从mq中读取消息
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cmqc.h>
#include "amqsget.h"
#include "commonfunc.h"
int mqsGet(unsigned char *sRecvBuffer)
{
MQOD
od =
{MQOD_DEFAULT};
MQMD
md =
{MQMD_DEFAULT};
MQGMO gmo =
{MQGMO_DEFAULT};
MQHCONN
Hcon;
MQHOBJ
Hobj;
MQLONG
O_options;
MQLONG
C_options;
MQLONG
CompCode;
MQLONG
OpenCode;
MQLONG
Reason;
MQLONG
CReason;
MQBYTE
buffer[65536];
MQLONG
buflen;
MQLONG
messlen;
char
QMName[50];
int iret =
0;
char
smqRecvQueNm[50];
char
smqSendQueNm[50];
char
smqQueMngNm[50];
char
smqQueOpenOptn[50];
char
smqQueClsOptn[50];
memset(smqRecvQueNm, 0,
sizeof(smqRecvQueNm));
memset(smqSendQueNm, 0,
sizeof(smqSendQueNm));
memset(smqQueMngNm, 0,
sizeof(smqQueMngNm));
memset(smqQueOpenOptn, 0,
sizeof(smqQueOpenOptn));
memset(smqQueClsOptn, 0,
sizeof(smqQueClsOptn));
memset(sRecvBuffer, 0, sizeof(buffer));
memset(buffer, 0, sizeof(buffer));
iret = ReadINIFile("MQMANAGELIST", "mqMngNm",
smqQueMngNm, "main.ini" );
if ( iret != 1 ) {
WriteLog("SEND",
"配置文件中没有找到节点[mqMngNm]\n");
return 1;
}
iret = ReadINIFile("MQMANAGELIST",
"mqQueOpenOptn", smqQueOpenOptn, "main.ini" );
if ( iret != 1 ) {
WriteLog("SEND",
"配置文件中没有找到节点[mqQueOpenOptn]\n");
return 1;
}
iret = ReadINIFile("MQMANAGELIST",
"mqQueClsOptn", smqQueClsOptn, "main.ini" );
if ( iret != 1 ) {
WriteLog("SEND",
"配置文件中没有找到节点[mqQueClsOptn]\n");
return 1;
}
iret = ReadINIFile("MQQUEUELIST", "mqRecvQueNm",
smqRecvQueNm, "main.ini" );
if ( iret != 1 ) {
WriteLog("SEND",
"配置文件中没有找到节点[mqRecvQueNm]\n");
return 1;
}
//WriteLog("SEND", "smqRecvQueNm=[%s],
smqQueMngNm=[%s], smqQueOpenOptn=[%s], smqQueClsOptn=[%s]\n\n",
smqRecvQueNm, smqQueMngNm, smqQueOpenOptn, smqQueClsOptn);
//WriteLog("SEND", "Sample AMQSGET0
start\n");
QMName[0] =
0;
strncpy(od.ObjectName, smqRecvQueNm,
MQ_Q_NAME_LENGTH);
strncpy(QMName, smqQueMngNm,
MQ_Q_MGR_NAME_LENGTH);
MQCONN(QMName,
&Hcon,
&CompCode,
&CReason);
if (CompCode == MQCC_FAILED)
{
WritePrintLog( "MQCONN ended
with reason code %d\n", CReason);
//exit( (int)CReason );
return 1;
}
if (0 != strlen(smqQueOpenOptn))
{
O_options = atoi(
smqQueOpenOptn );
//WriteLog("SEND",
"open options are %d\n", O_options);
}
else
{
O_options =
MQOO_INPUT_AS_Q_DEF
| MQOO_FAIL_IF_QUIESCING
;
}
MQOPEN(Hcon,
&od,
O_options,
&Hobj,
&OpenCode,
&Reason);
if (Reason != MQRC_NONE)
{
WritePrintLog( "MQOPEN ended
with reason code %d\n", Reason);
}
if (OpenCode == MQCC_FAILED)
{
WritePrintLog( "unable to open
queue for input\n");
return 1;
}
CompCode =
OpenCode;
gmo.Options =
MQGMO_WAIT
|
MQGMO_NO_SYNCPOINT
|
MQGMO_CONVERT;
gmo.WaitInterval =
15000;
//while (CompCode != MQCC_FAILED)
if (CompCode != MQCC_FAILED)
{
buflen = sizeof(buffer) -
1;
memcpy(md.MsgId, MQMI_NONE,
sizeof(md.MsgId));
memcpy(md.CorrelId, MQCI_NONE,
sizeof(md.CorrelId));
md.Encoding
= MQENC_NATIVE;
md.CodedCharSetId =
MQCCSI_Q_MGR;
MQGET(Hcon,
Hobj,
&md,
&gmo,
buflen,
buffer,
&messlen,
&CompCode,
&Reason);
if (Reason != MQRC_NONE)
{
if (Reason ==
MQRC_NO_MSG_AVAILABLE)
{
WritePrintLog(
"no more messages\n");
}
else
{
WritePrintLog(
"MQGET ended with reason code %d\n", Reason);
if
(Reason == MQRC_TRUNCATED_MSG_FAILED)
{
CompCode
= MQCC_FAILED;
}
}
}
if (CompCode !=
MQCC_FAILED)
{
buffer[messlen]
=
'\0';
WritePrintLog(
"message <%s>\n", buffer);
strcpy(sRecvBuffer,
buffer);
}
}
if (OpenCode != MQCC_FAILED)
{
if (0 !=
strlen(smqQueClsOptn))
{
C_options =
atoi( smqQueClsOptn );
WritePrintLog(
"close options are %d\n", C_options);
}
else
{
C_options =
MQCO_NONE;
}
MQCLOSE(Hcon,
&Hobj,
C_options,
&CompCode,
&Reason);
if (Reason != MQRC_NONE)
{
WritePrintLog(
"MQCLOSE ended with reason code %d\n", Reason);
}
}
if (CReason != MQRC_ALREADY_CONNECTED )
{
MQDISC(&Hcon,
&CompCode,
&Reason);
if (Reason != MQRC_NONE)
{
WritePrintLog(
"MQDISC ended with reason code %d\n", Reason);
}
}
if(0 == strlen(buffer)) {
//WritePrintLog("接收到的报文为空\n");
return 1;
}
else {
WritePrintLog("buffer=[%s]\n",
buffer);
strcpy(sRecvBuffer,
buffer);
}
//WriteLog("SEND", "Sample AMQSGET0
end\n");
return(0);
}
amqsput.c 向mq中发送消息
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cmqc.h>
#include "amqsput.h"
#include "commonfunc.h"
#include "codeConv.h"
int mqsPut(char * sOutBuffer)
{
FILE *fp;
MQOD
od =
{MQOD_DEFAULT};
MQMD
md =
{MQMD_DEFAULT};
MQPMO pmo =
{MQPMO_DEFAULT};
MQHCONN
Hcon;
MQHOBJ
Hobj;
MQLONG
O_options;
MQLONG
C_options;
MQLONG
CompCode;
MQLONG
OpenCode;
MQLONG
Reason;
MQLONG
CReason;
MQLONG
messlen;
char
buffer[65535];
char
QMName[50];
WritePrintLog("Sample AMQSPUT0
start\n");
int iret = 0;
char sQMName[50];
char sQInName[50];
char sQOutName[50];
char smqQueOpenOptn[50];
char smqQueClsOptn[50];
char sSendDynamicQueNm[50];
memset(sQMName, 0, sizeof(sQMName));
memset(sQInName, 0, sizeof(sQInName));
memset(sQOutName, 0, sizeof(sQOutName));
memset(smqQueOpenOptn, 0,
sizeof(smqQueOpenOptn));
memset(smqQueClsOptn, 0,
sizeof(smqQueClsOptn));
memset(sSendDynamicQueNm, 0,
sizeof(sSendDynamicQueNm));
iret = ReadINIFile("MQMANAGELIST", "mqMngNm",
sQMName, "main.ini" );
if ( iret != 1 ) {
WritePrintLog(
"配置文件中没有找到节点[mqMngNm]\n");
return 1;
}
iret = ReadINIFile("MQQUEUELIST", "mqRecvQueNm",
sQInName, "main.ini" );
if ( iret != 1 ) {
WritePrintLog(
"配置文件中没有找到节点[mqRecvQueNm]\n");
return 1;
}
iret = ReadINIFile("MQQUEUELIST", "mqSendQueNm",
sQOutName, "main.ini" );
if ( iret != 1 ) {
WritePrintLog(
"配置文件中没有找到节点[mqSendQueNm]\n");
return 1;
}
iret = ReadINIFile("MQQUEUELIST",
"mqSendDynamicQueNm", sSendDynamicQueNm, "main.ini" );
if ( iret != 1 ) {
WritePrintLog(
"配置文件中没有找到节点[mqSendDynamicQueNm]\n");
return 1;
}
iret = ReadINIFile("MQMANAGELIST",
"mqQueOpenOptn", smqQueOpenOptn, "main.ini" );
if ( iret != 1 ) {
WriteLog("SEND",
"配置文件中没有找到节点[mqQueOpenOptn]\n");
return 1;
}
iret = ReadINIFile("MQMANAGELIST",
"mqQueClsOptn", smqQueClsOptn, "main.ini" );
if ( iret != 1 ) {
WriteLog("SEND",
"配置文件中没有找到节点[mqQueClsOptn]\n");
return 1;
}
WritePrintLog( "sQMName=[%s],
sQOutName=[%s],sSendDynamicQueNm=[%s],sQInName=[%s]\n\n", sQMName,
sQOutName,sSendDynamicQueNm, sQInName);
QMName[0] =
0;
strncpy(QMName, sQMName,
(size_t)MQ_Q_MGR_NAME_LENGTH);
MQCONN(QMName,
&Hcon,
&CompCode,
&CReason);
if (CompCode == MQCC_FAILED)
{
WritePrintLog("MQCONN ended
with reason code %d\n", CReason);
exit( (int)CReason );
}
strncpy(od.ObjectName, sQOutName,
(size_t)MQ_Q_NAME_LENGTH);
WritePrintLog("target queue is %s\n",
od.ObjectName);
//if (argc > 5)
{
strncpy(od.ObjectQMgrName, QMName, (size_t)
MQ_Q_MGR_NAME_LENGTH);
WritePrintLog("target queue manager is %s\n",
od.ObjectQMgrName);
}
//if (argc > 6)
{
strncpy(od.DynamicQName, sSendDynamicQueNm,
(size_t) MQ_Q_NAME_LENGTH);
WritePrintLog("dynamic queue name is %s\n",
od.DynamicQName);
}
if (0 != strlen(smqQueOpenOptn))
{
O_options = atoi(
smqQueOpenOptn );
WritePrintLog("open
options are %d\n", O_options);
}
else
{
O_options =
MQOO_OUTPUT
|
MQOO_FAIL_IF_QUIESCING
;
}
MQOPEN(Hcon,
&od,
O_options,
&Hobj,
&OpenCode,
&Reason);
if (Reason != MQRC_NONE)
{
WritePrintLog("MQOPEN ended
with reason code %d\n", Reason);
}
if (OpenCode == MQCC_FAILED)
{
WritePrintLog("unable to open
queue for output\n");
}
CompCode =
OpenCode;
fp = stdin;
memcpy(md.Format,
MQFMT_STRING,
(size_t)MQ_FORMAT_LENGTH);
pmo.Options = MQPMO_NO_SYNCPOINT
| MQPMO_FAIL_IF_QUIESCING;
if (CompCode != MQCC_FAILED)
{
//if (fgets(buffer,
sizeof(buffer), fp) != NULL)
//{
// messlen =
(MQLONG)strlen(buffer);
//if (buffer[messlen-1] ==
'\n')
//{
//
buffer[messlen-1] =
'\0';
//
--messlen;
//}
// }
//else messlen =
0;
strcpy(buffer,
sOutBuffer);
messlen = strlen(buffer);
char *szOut;
char sTbuffer[65535];
memset(sTbuffer, 0,
sizeof(sTbuffer));
szOut = u2g(buffer);
strcpy(sTbuffer, szOut);
free(szOut);
if (messlen >
0)
{
WritePrintLog("MQPUT===============buffer[%s][%d]\n",
sTbuffer, strlen(sTbuffer));
memcpy(md.MsgId,
MQMI_NONE,
sizeof(md.MsgId) );
MQPUT(Hcon,
Hobj,
&md,
&pmo,
messlen,
buffer,
&CompCode,
&Reason);
if (Reason !=
MQRC_NONE)
{
WritePrintLog("MQPUT
ended with reason code %d\n", Reason);
}
}
else
CompCode =
MQCC_FAILED;
}
if (OpenCode != MQCC_FAILED)
{
if (0 !=
strlen(smqQueClsOptn))
{
C_options =
atoi( smqQueClsOptn );
WritePrintLog("close
options are %d\n", C_options);
}
else
{
C_options =
MQCO_NONE;
}
MQCLOSE(Hcon,
&Hobj,
C_options,
&CompCode,
&Reason);
if (Reason != MQRC_NONE)
{
WritePrintLog("MQCLOSE
ended with reason code %d\n", Reason);
}
}
if (CReason != MQRC_ALREADY_CONNECTED)
{
MQDISC(&Hcon,
&CompCode,
&Reason);
if (Reason != MQRC_NONE)
{
WritePrintLog("MQDISC
ended with reason code %d\n", Reason);
}
}
WritePrintLog("Sample AMQSPUT0 end\n");
return(0);
}
C语言实现mq收发数据的函数的更多相关文章
- dsp28377控制DM9000收发数据——第三版程序,通过外部引脚触发来实现中断接受数据,优化掉帧现象
//-------------------------------------------------------------------------------------------- - //D ...
- dsp28377控制DM9000收发数据
首先感谢上一篇转载文章的作者给出的参考,下面是一些自己在调试过程中的一些步骤: 首先把代码贴上来: //------------------------------------------------ ...
- 【C语言入门教程】5.2 函数的作用域规则(auto, static)
作用域规则是指代码或数据的有效使用范围.C语言将函数作为独立的代码块,函数之间不能相互访问其内部的代码或数据.函数间数据的传递只能通过接口实现.但是,变量的定义方法可改变函数的作用域规则,可将变量分为 ...
- iOS开发之音频口通信-通过方波来收发数据
之前做过的项目有需要通过音频口通信用方波来收发数据,由于这方面的资料比较少,下面就介绍下其原理,希望能给大家帮助. 一. 音频通信简介大家应该都知道支付宝声波支付和拉卡拉吧,它们都是利用手机的音频口( ...
- 【AT91SAM3S】串口UART初始化及收发数据
SAM3S中的UART串口是一个两线异步收发器.这个串口能用来通信或者跟踪.有两个DMA通道与UART串口关联,可通过使用DMA处理串口传输以节省CPU时间. SAM3S4C中有两个UART.与外设引 ...
- R语言中的横向数据合并merge及纵向数据合并rbind的使用
R语言中的横向数据合并merge及纵向数据合并rbind的使用 我们经常会遇到两个数据框拥有相同的时间或观测值,但这些列却不尽相同.处理的办法就是使用merge(x, y ,by.x = ,by.y ...
- R语言系列:生成数据
R语言系列:生成数据 (2014-05-04 17:41:57) 转载▼ 标签: r语言 教育 分类: 生物信息 生成规则数据1.使用“:“,如x=1:10,注意该方法既可以递增也可以递减,如y=10 ...
- 孤荷凌寒自学python第四十八天通用同一数据库中复制数据表函数最终完成
孤荷凌寒自学python第四十八天通用同一数据库中复制数据表函数最终完成 (完整学习过程屏幕记录视频地址在文末) 今天继续建构自感觉用起来顺手些的自定义模块和类的代码. 今天经过反复折腾,最终基本上算 ...
- 孤荷凌寒自学python第四十七天通用跨数据库同一数据库中复制数据表函数
孤荷凌寒自学python第四十七天通用跨数据库同一数据库中复制数据表函数 (完整学习过程屏幕记录视频地址在文末) 今天继续建构自感觉用起来顺手些的自定义模块和类的代码. 今天打算完成的是通用的(至少目 ...
随机推荐
- spring cloud: 使用consul来替换config server
上一篇提到了,eureka 2.x官方停止更新后,可以用consul来替代,如果采用consul的话,其实config server也没必要继续使用了,consul自带kv存储,完全可以取代confi ...
- webbench进行压力测试
参考原文:http://www.vpser.net/opt/webserver-test.html webbench是Linux下的一个网站压力测试工具,最多可以模拟3万个并发连接去测试网站的负载能力 ...
- C# DES (ECB模式) 加密解密 --单倍长
加密: 调用时: Encrypt_DES16("2AF349243535BCD3", "1111111111111111"); public static s ...
- centos 终端界面代理设置
一.centos自带界面设置代理 1. 界面设置 squid默认代理端口3128. 2. firefox设置 设置 -> 局域网设置 -> ip:port / username:passw ...
- mac pro 如何让终端默认运行python3.X而不是2.7
Mac版本的Python默认是2.7,安装高版本后需要修改为你安装的版本. 1,首先打开终端 open ~/.bash_profile 打开配置文件 2. 写入python的外部环境变量(本人的版本是 ...
- stm32型号解读
ST意法半导体在牵手ARM后可以说是做的非常成功,抓住了从普通MCU到ARM的市场转变的机会.由于ST公司的STM32系列ARM 使用了完善的库开发,作为芯片的应用者不用从底层的寄存器来实现每个功 ...
- oracle获取今年在内的前几年、后几年
前几年 select to_char(sysdate, 'yyyy') - level + 1 years from dual connect by level <= num num:即想获取几 ...
- openHEVC 编译 for VS2017+Win10 x64
编译暂未成功,有空再次更新 前期准备: yasm下载:http://yasm.tortall.net/Download.html http://www.tortall.net/projects/yas ...
- IDEA + TortoiseSVN 导入 Eclipse 项目
IDEA 是目前最好用的 Java 开发工具,所以我一直用的 IDEA,但是项目是由 Eclipse 创建,并且项目中一部份成员用 Eclipse.由于版本控制一直用的小乌龟(TortoiseSVN) ...
- 实例展示elasticsearch集群生态,分片以及水平扩展.
elasticsearch用于构建高可用和可扩展的系统.扩展的方式可以是购买更好的服务器(纵向扩展)或者购买更多的服务器(横向扩展),Elasticsearch能从更强大的硬件中获得更好的性能,但是纵 ...