ASG
IBM
Zystems
Cressida
Icon
Netflexity
 
  MQSeries.net
Search  Search       Tech Exchange      Education      Certifications      Library      Info Center      SupportPacs      LinkedIn  Search  Search                                                                   FAQ  FAQ   Usergroups  Usergroups
 
Register  ::  Log in Log in to check your private messages
 
RSS Feed - WebSphere MQ Support RSS Feed - Message Broker Support

MQSeries.net Forum Index » IBM MQ API Support » Connect to remote MQ queue manager from C client

Post new topic  Reply to topic
 Connect to remote MQ queue manager from C client « View previous topic :: View next topic » 
Author Message
simyobs
PostPosted: Mon Sep 14, 2009 1:32 pm    Post subject: Connect to remote MQ queue manager from C client Reply with quote

Newbie

Joined: 15 Sep 2008
Posts: 5

Hi All,
I'm trying to to build a MQClient application using C program to put messages to a local queue using Channel Defenition Structure.

The application is standalone using MQ client bindings and connecting to Qmgr on other server. The queue exist local not remote

I have tried this a hundred times and gotten the same result mqrc 2045, Which is "option not valid for object type". The options I tried with were MQOO_OUTPUT | MQOO_FAIL_IF_QUIESCING.


I would really,really appreciate any help anyone can extend...
Here's the sample code:

#include <cmqc.h> /* MQ Header Files basic and */
#include <cmqxc.h> /* Channel defs */
#include <stdio.h> /* IO header */
#include <string.h> /* String functions */

int main()

{
MQCHAR qmgrName[MQ_Q_MGR_NAME_LENGTH];
MQHCONN hConn; /* Connection handle */
MQHOBJ hObject; /* Object handle */
MQLONG Selector; /* selector for inquiry */
MQLONG iCompCode; /* Completion code */
MQLONG iReason; /* Reason code */
MQLONG iMessageLen; /* Messsage Length */
MQBYTE buffer[1000]; /* Message data buffer */
MQLONG iOpenOptions ;



/* These are the MQ Structs to be used in this program */

MQOD mqod = {MQOD_DEFAULT}; /* Object description */

MQMD mqmd = {MQMD_DEFAULT}; /* Message Descriptor */

MQPMO mqpmo = {MQPMO_DEFAULT}; /* Put message options */

MQGMO gmo = {MQGMO_DEFAULT}; /* get message options */
MQOD od = {MQOD_DEFAULT}; /* Object Descriptor */


/* These are the special structures used */

MQCNO mqcno = {MQCNO_DEFAULT} ; /* Connection options */
MQCD mqcd = {MQCD_CLIENT_CONN_DEFAULT}; /* Channel Defs */



/* Defined in cmqxc.h */


strncpy(qmgrName, "XYZQmgr", MQ_Q_MGR_NAME_LENGTH);
printf("Connecting to queue manager %-48.48s\n", qmgrName);


strncpy(mqcd.ConnectionName,
"10.100.1.1",
MQ_CONN_NAME_LENGTH);

strncpy(mqcd.ChannelName,
"SYSTEM.DEF.SVRCONN",
MQ_CHANNEL_NAME_LENGTH);

/* Pointing the MQCNO to the client connection definition*/

mqcno.ClientConnPtr = &mqcd;
mqcno.Version = MQCNO_VERSION_2;


printf("Connecting to using Channel %-48.48s\n", mqcno.ClientConnPtr);


/* MQCONNX - (Queue Manager Name, MQCNO, Connection handle, Completion Reason code ) */

printf("Before MQCONNX!!!!\n");
qmgrName[0] = '\0';
MQCONNX(qmgrName,
&mqcno,
&hConn,
&iCompCode,
&iReason);

if (iCompCode == MQCC_FAILED)
{
printf("MQCONN failed with reason code %ld\n", iReason);
exit( (int)iReason);
/* return(iReason);*/
}

/* iOpenOptions = MQOO_OUTPUT | MQOO_FAIL_IF_QUIESCING;*/
printf("Connecting to OpenOptions %-48.48s\n",mqod.ObjectName);

strncpy(mqod.ObjectName, "AA", MQ_Q_NAME_LENGTH);

od.ObjectType = MQOT_Q_MGR; /* open the queue manager object*/

/* MQOPEN */
printf("Before MQOPEN!!!!\n");
MQOPEN(hConn,
&od,
MQOO_OUTPUT + MQOO_FAIL_IF_QUIESCING,
&hObject,
&iCompCode,
&iReason);

if (iCompCode == MQCC_FAILED)
{
printf("MQOPEN failed with reason code %ld\n", iReason);
return(iReason);

}


strcpy (buffer , "Hello from Client") ;
memcpy (mqmd.Format , "MQSTR " , 8 );
iMessageLen = strlen(buffer) ;

MQPUT(hConn,
hObject,
&mqmd,
&mqpmo,
iMessageLen,
buffer,
&iCompCode,
&iReason);

if (iCompCode == MQCC_FAILED)
{
printf("MQPUT failed with reason code %ld\n", iReason);
return(iReason);

}

/* MQCLOSE */

MQCLOSE(hConn, &hObject, MQCO_NONE, &iCompCode, &iReason);

/* MQDISC - Do not forget to use DISC !!! This is Client Code */

MQDISC(&hConn, &iCompCode, &iReason);

return(0);
}

Thanks : javascript:emoticon('')
Back to top
View user's profile Send private message
fjb_saper
PostPosted: Mon Sep 14, 2009 1:40 pm    Post subject: Reply with quote

Grand High Poobah

Joined: 18 Nov 2003
Posts: 20756
Location: LI,NY

You should really look at some of the examples delivered.
If they are not installed ask for them to be installed.
From a cursory look, and I am not a C person, it looks like you are assigning queue options to a qmgr.... not suitable....
_________________
MQ & Broker admin
Back to top
View user's profile Send private message Send e-mail
Vitor
PostPosted: Mon Sep 14, 2009 3:37 pm    Post subject: Reply with quote

Grand High Poobah

Joined: 11 Nov 2005
Posts: 26093
Location: Texas, USA

fjb_saper wrote:
From a cursory look, and I am not a C person, it looks like you are assigning queue options to a qmgr.... not suitable....




You're opening the wrong thing. Check the samples (as my most worthy associate says), and the Application Programming Guide.
_________________
Honesty is the best policy.
Insanity is the best defence.
Back to top
View user's profile Send private message
gbaddeley
PostPosted: Mon Sep 14, 2009 7:36 pm    Post subject: Reply with quote

Jedi Knight

Joined: 25 Mar 2003
Posts: 2538
Location: Melbourne, Australia

Code:
od.ObjectType = MQOT_Q_MGR; /* open the queue manager object*/


Take this line out and try again. Because you have initialised:

Code:
MQOD od = {MQOD_DEFAULT}; /* Object Descriptor */


It will use the default object type, which is MQOT_Q, a Queue.
_________________
Glenn
Back to top
View user's profile Send private message
bruce2359
PostPosted: Tue Sep 15, 2009 5:52 am    Post subject: Reply with quote

Poobah

Joined: 05 Jan 2008
Posts: 9471
Location: US: west coast, almost. Otherwise, enroute.

Quote:
od.ObjectType = MQOT_Q_MGR; /* open the queue manager object*/

This statement indicates that you are trying to open the queuemanager object, not a queue object. Open options must be appropriate for the object type - as the error message you are receiving says.

Read the WMQ Application Programming Ref manual about open options.
_________________
I like deadlines. I like to wave as they pass by.
ב''ה
Lex Orandi, Lex Credendi, Lex Vivendi. As we Worship, So we Believe, So we Live.
Back to top
View user's profile Send private message
RogerLacroix
PostPosted: Tue Sep 15, 2009 8:44 am    Post subject: Reply with quote

Jedi Knight

Joined: 15 May 2001
Posts: 3264
Location: London, ON Canada

Hi,

In the future, use BBCode of "code" to make your code readable.

Here's a simple C program to do what you are trying to do:
Code:
/*
 * Program Name
 *  MQTest11.c
 *
 * Description
 * Test program to connect to a queue manager using MQCONNX and
 * then put a message to a queue.
 *
 * Sample Command Line Parameters
 *   QMgrName ChlName hostname(port) QName
 *
 * Where
 *    QMgrName is the queue manager name
 *    ChlName is the name of the channel to be used
 *    hostname(port) is the hostname and port number
 *    QName is the queue name
 * i.e.
 *    MQTest11.exe MQWT1 MY.TEST.EXIT 127.0.0.1(1415) TEST.Q1
 *
 * @author Roger Lacroix, Capitalware Inc.
 * @version 1.0
 * @license Apache 2 License
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <cmqc.h>            /* includes for MQI          */
#include <cmqxc.h>           /* For MQCD definition       */

/*
 * mainline
 */
int main(int argc, char **argv)
{
   /* --------------------------------------------
    * Variable declarations.
    * --------------------------------------------
    */
   MQCNO   Connect_options = {MQCNO_DEFAULT};       /* MQCONNX options           */
   MQCD    ClientConn = {MQCD_CLIENT_CONN_DEFAULT}; /* Client connection channel */
   MQHCONN  Hcon;                   /* connection handle             */
   MQHOBJ   Hobj;                   /* object handle                 */
   MQLONG   CompCode;               /* completion code               */
   MQLONG   Reason;                 /* reason code                   */
   MQOD     od = {MQOD_DEFAULT};    /* Object Descriptor             */
   MQMD     md = {MQMD_DEFAULT};    /* Message Descriptor            */
   MQPMO    pmo = {MQPMO_DEFAULT};  /* put message options           */
   MQLONG   O_options;              /* MQOPEN options                */
   MQLONG   C_options;              /* MQCLOSE options               */

   char     QMgrName[MQ_Q_MGR_NAME_LENGTH+1];
   char     QName[MQ_Q_NAME_LENGTH+1];
   char     channelName[MQ_CHANNEL_NAME_LENGTH+1];
   char     hostname[1024];
   MQLONG   buflen;                 /* buffer length                 */
   char     TempBuf[1024] = "This is a simple test message.";

   /* --------------------------------------------
    * Code section
    * --------------------------------------------
    */
   if (argc != 5)
   {
      printf("Usage: MQTest11 QMgrName ChlName hostname(port) QName\n");
      return(1);
   }

   printf("MQTest11 started\n");

   strncpy(QMgrName, argv[1], MQ_Q_MGR_NAME_LENGTH);
   QMgrName[MQ_Q_MGR_NAME_LENGTH] = '\0';

   strncpy(channelName, argv[2], MQ_CHANNEL_NAME_LENGTH);
   channelName[MQ_CHANNEL_NAME_LENGTH] = '\0';

   strncpy(hostname, argv[3], 1023);
   hostname[1023] = '\0';

   strncpy(QName, argv[4], MQ_Q_NAME_LENGTH);
   QMgrName[MQ_Q_NAME_LENGTH] = '\0';

   printf("Using values:\n");
   printf("   QMgrName   : %s\n", QMgrName);
   printf("   QName      : %s\n", QName);
   printf("   ChannelName: %s\n", channelName);
   printf("   hostname   : %s\n", hostname);

   /*
    * Set required values for MQCONNX
    */
   strncpy(ClientConn.ConnectionName,
           hostname,
           MQ_CONN_NAME_LENGTH);

   strncpy(ClientConn.ChannelName,
           channelName,
           MQ_CHANNEL_NAME_LENGTH);

   /* Point the MQCNO to the client connection definition */
   Connect_options.ClientConnPtr = &ClientConn;
   Connect_options.Version = MQCNO_VERSION_2;

   /*
    * Connect to queue manager
    */
   MQCONNX(QMgrName,                /* queue manager                  */
           &Connect_options,        /* options for connection         */
           &Hcon,                   /* connection handle              */
           &CompCode,               /* completion code                */
           &Reason);                /* reason code                    */

   /* report reason and stop if it failed     */
   if (CompCode == MQCC_FAILED)
   {
      printf("MQTest11 MQCONN ended with reason code %d\n", Reason);
   }
   else
   {
      printf("MQTest11 Sucessfully connected\n");

      strncpy(od.ObjectName, QName, (size_t)MQ_Q_NAME_LENGTH);
      O_options = MQOO_OUTPUT + MQOO_FAIL_IF_QUIESCING;

      MQOPEN(Hcon,                      /* connection handle            */
             &od,                       /* object descriptor for queue  */
             O_options,                 /* open options                 */
             &Hobj,                     /* object handle                */
             &CompCode,                 /* MQOPEN completion code       */
             &Reason);                  /* reason code                  */

     printf("MQTest11 MQOPEN CC=%ld RC=%ld\n", CompCode, Reason);

      if (CompCode == MQCC_OK)
      {
         buflen = strlen(TempBuf);

         md.MsgType  = MQMT_DATAGRAM;
         memcpy(md.MsgId, MQMI_NONE, sizeof(md.MsgId));
         memcpy(md.CorrelId, MQCI_NONE, sizeof(md.CorrelId));
         memcpy(md.Format, MQFMT_STRING, (size_t)MQ_FORMAT_LENGTH);

         MQPUT(Hcon,                /* connection handle               */
               Hobj,                /* object handle                   */
               &md,                 /* message descriptor              */
               &pmo,                /* default options (datagram)      */
               buflen,              /* buffer length                   */
               TempBuf,             /* message buffer                  */
               &CompCode,           /* completion code                 */
               &Reason);            /* reason code                     */

         printf("MQTest11 MQPUT CC=%ld RC=%ld\n", CompCode, Reason);

         C_options = 0;                  /* no close options             */
         MQCLOSE(Hcon,                   /* connection handle            */
                 &Hobj,                   /* object handle               */
                 C_options,
                 &CompCode,               /* completion code             */
                 &Reason);                /* reason code                 */

        printf("MQTest11 MQCLOSE CC=%ld RC=%ld\n", CompCode, Reason);
      }

      /*
       * Disconnect from queue manager
       */
      MQDISC(&Hcon,                     /* connection handle          */
             &CompCode,                 /* completion code            */
             &Reason);                  /* reason code                */

      printf("MQTest11 MQDISC CC=%ld RC=%ld\n", CompCode, Reason);
   }

   printf("MQTest11 ended\n");
   return(0);
}


Regards,
Roger Lacroix
Capitalware Inc.
_________________
Capitalware: Transforming tomorrow into today.
Connected to MQ!
Twitter
Back to top
View user's profile Send private message Visit poster's website
simyobs
PostPosted: Wed Sep 16, 2009 6:25 pm    Post subject: Reply with quote

Newbie

Joined: 15 Sep 2008
Posts: 5

Thanks for the sample and advice.

I tried using the sample code and it returned mqrc 2058, Qmgr not available.
The listener & Qmgr both are running, also to confirm did a telnet and the port is connected.
Code:

$ testmq RemoteQmgr SYSTEM.DEF.SVRCONN 10.100.1.1 AA
MQTest11 started
Using values:
   QMgrName   : RemoteQmgr
   QName      : AA
   ChannelName: SYSTEM.DEF.SVRCONN
   hostname   : 10.100.1.1
MQTest11 MQCONN ended with reason code 2058
MQTest11 ended


However, if run the code locally, the sample is able to get Connected.
So I'm puzzle why? Is there some thing missing !

Appreciate your reply.('')

Code:

$ testmq localQmgr SYSTEM.DEF.SVRCONN localhost AA
MQTest11 started
Using values:
   QMgrName   : localQmgr
   QName      : AA
   ChannelName: SYSTEM.DEF.SVRCONN
   hostname   : localhost
MQTest11 Sucessfully connected
MQTest11 MQOPEN CC=0 RC=0
MQTest11 MQPUT CC=0 RC=0
MQTest11 MQCLOSE CC=0 RC=0
MQTest11 MQDISC CC=0 RC=0
MQTest11 ended
Back to top
View user's profile Send private message
gunter
PostPosted: Wed Sep 16, 2009 9:43 pm    Post subject: Reply with quote

Partisan

Joined: 21 Jan 2004
Posts: 307
Location: Germany, Frankfurt

Check if the name of the queuemanager is really RemoteQmgr and not REMOTEQMGR.
_________________
Gunter Jeschawitz
IBM Certified System Administrator - Websphere MQ, 5.3
Back to top
View user's profile Send private message
bruce2359
PostPosted: Thu Sep 17, 2009 5:14 am    Post subject: Reply with quote

Poobah

Joined: 05 Jan 2008
Posts: 9471
Location: US: west coast, almost. Otherwise, enroute.

Quote:
Using values:
QMgrName : RemoteQmgr
QName : AA
ChannelName: SYSTEM.DEF.SVRCONN
hostname : 10.100.1.1
MQTest11 MQCONN ended with reason code 2058
MQTest11 ended



However, if run the code locally, the sample is able to get Connected.
So I'm puzzle why? Is there some thing missing !

Appreciate your reply.('')

Code:

$ testmq localQmgr SYSTEM.DEF.SVRCONN localhost AA
MQTest11 started
Using values:
QMgrName : localQmgr
QName : AA
ChannelName: SYSTEM.DEF.SVRCONN
hostname : localhost


What is the real name of the qmgr?
_________________
I like deadlines. I like to wave as they pass by.
ב''ה
Lex Orandi, Lex Credendi, Lex Vivendi. As we Worship, So we Believe, So we Live.
Back to top
View user's profile Send private message
RogerLacroix
PostPosted: Thu Sep 17, 2009 7:46 pm    Post subject: Reply with quote

Jedi Knight

Joined: 15 May 2001
Posts: 3264
Location: London, ON Canada

Hi,

I bet you linked the application for bindings mode rather than for client mode. Please look up in the WMQ Application Programming Guide how to compile and link a C application for client mode.

Regards,
Roger Lacroix
Capitalware Inc.
_________________
Capitalware: Transforming tomorrow into today.
Connected to MQ!
Twitter
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic  Reply to topic Page 1 of 1

MQSeries.net Forum Index » IBM MQ API Support » Connect to remote MQ queue manager from C client
Jump to:  



You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Protected by Anti-Spam ACP
 
 


Theme by Dustin Baccetti
Powered by phpBB © 2001, 2002 phpBB Group

Copyright © MQSeries.net. All rights reserved.