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);
}