apidemo_sgip.c
上传用户:xd_1999
上传日期:2007-05-02
资源大小:2286k
文件大小:9k
源码类别:

手机短信编程

开发平台:

Java

  1. #include    <stdio.h>
  2. #include  <stdlib.h>
  3. #include    <netinet/in.h>
  4. #include    "proxy_api.h"
  5. typedef unsigned long ULONG;
  6. typedef struct tagPureSgipHead
  7. {
  8. ULONG nLen;
  9. ULONG nCmdID;
  10. ULONG nNodeNum;
  11. ULONG nTime;
  12. ULONG nSequenceID;
  13. }tagPureSgipHead;
  14. typedef tagPureSgipHead tagSgipHead;
  15. #define SGIP_DELIVER           0x00000004
  16. #define SGIP_REPORT               0x00000005
  17. main(argc,argv)
  18. int argc;
  19. char *argv[]; 
  20. {
  21.     int  ret;
  22.     int  i, j;
  23.     /* 
  24.     ** 发送短消息所需要的参数 
  25.     */
  26.     char psSPNumber[22];
  27.     char psChargeNumber[22];
  28.     unsigned char byUserCount;
  29.     char psUserNumber[2100];
  30.     char psCorpId[6];
  31.     char psServiceType[11];
  32.     unsigned char byFeeType;
  33.     char psFeeValue[7];
  34.     char psGivenValue[7];
  35.     unsigned char byAgentFlag;
  36.     unsigned char byMTFlag;
  37.     unsigned char byPriority;
  38.     char psExpireTime[17];
  39.     char psScheduleTime[17];
  40.     unsigned char byReportFlag;
  41.     unsigned char byTPPID;
  42.     unsigned char byTPUDHI;
  43.     unsigned char byMsgCoding;
  44.     unsigned char byMsgType;
  45.     unsigned int nMsgLen;
  46.     char psMsgContent[161];
  47.     char pReserve[9];
  48.     unsigned char  *tmpPsz;
  49.     /* 
  50.     ** 消息发送的序列号,用于配对发送消息以及其应答消息, 由应用程序保证其唯一 
  51.     */
  52.     unsigned short  LID, sLid;
  53.     /* 
  54.     ** 接收提交消息的应答消息所需要的缓冲参数 
  55.     */
  56.     int             iStatus;    
  57.     char pSubmitAckReserve[9];
  58.     /* 
  59.     ** 接收短消息所需要的缓冲参数 
  60.     */
  61.     /*
  62.     char psDeliverUserNumber[22];
  63.     char psDeliverSPNumber[22];
  64.     unsigned char byDeliverTPPID;
  65.     unsigned char byDeliverTPUDHI;
  66.     unsigned char byDeliverMsgCoding;
  67.     unsigned int nDeliverMsgLen;
  68.     char psDeliverMsgContent[161];
  69.     char psDeliverReserve[9];
  70.     */
  71.     /*
  72.     ** 用于状态报告的缓冲区
  73.     */
  74.     unsigned long   ulOrgNodeNum;
  75.     unsigned long   ulOrgTimeStamp;
  76.     unsigned long   ulOrgSequence;    
  77.     unsigned char   byReportType;
  78.     char            psReportUserNum[22];
  79.     unsigned char   bystate;
  80.     unsigned char   byErrCode;
  81.     char            psReportReserve[9];
  82.     char sMsg[2048];
  83.     int  nLen;
  84.     LID = 0;    
  85.     /* 
  86.     ** 连接到短消息代理服务器上 
  87.     */
  88.     ret = proxy_login(1);
  89.     if(ret == SUCCESS)
  90.         printf("nLogin Success!");
  91.     else if(ret == FAILURE)
  92.         printf("nLogin Fail!");
  93.     else
  94.         printf("nFatal Error!");
  95.     for(;;)
  96.     {
  97.         /******************************************************
  98.         ** 提交短消息 
  99.         *******************************************************/
  100.         LID ++;           /* 应用程序自身的序列号,请自己保证唯一,建议采用++操作 */
  101.         strcpy(psSPNumber, "8888");
  102.         strcpy(psChargeNumber, "13301001234");
  103.         byUserCount = 1;
  104.         strcpy(psUserNumber, "13301001234");
  105.         strcpy(psCorpId, "12345");
  106.         strcpy(psServiceType, "TPCQ");
  107.         byFeeType = 2;
  108.         strcpy(psFeeValue, "20");
  109.         strcpy(psGivenValue, "0");
  110.         byAgentFlag = 0;
  111.         byMTFlag = 0;
  112.         byPriority = 0;
  113.         psExpireTime[0] = 0;
  114.         psScheduleTime[0] = 0;
  115.         byReportFlag = 1;
  116.         byTPPID = 0;
  117.         byTPUDHI = 0;
  118.         byMsgCoding = 8;
  119.         byMsgType = 0;        
  120.         strcpy(psMsgContent, "测试短消息12345");
  121.         nMsgLen = strlen(psMsgContent);        
  122.         
  123.         /* 保留字段,请填为空 */
  124.         strcpy(pReserve, "");
  125.         
  126.         ret = SGIPSnd(
  127.             psSPNumber,
  128.             psChargeNumber,
  129.             byUserCount,
  130.             psUserNumber,
  131.             psCorpId,
  132.             psServiceType,
  133.             byFeeType,
  134.             psFeeValue,
  135.             psGivenValue,
  136.             byAgentFlag,
  137.             byMTFlag,
  138.             byPriority,
  139.             psExpireTime,
  140.             psScheduleTime,
  141.             byReportFlag,
  142.             byTPPID,
  143.             byTPUDHI,
  144.             byMsgCoding,
  145.             byMsgType,
  146.             nMsgLen,
  147.             psMsgContent,
  148.             pReserve,
  149.             LID);
  150.         
  151. if( ret == SUCCESS)
  152.             printf("nnSubmit SM Success!");
  153.         else if(ret == FAILURE)
  154.         {
  155.             printf("nSubmit SM Fail!");
  156.             getchar();
  157.         }
  158.         else
  159.         {
  160.             printf("nFatal Error!");
  161.             getchar();
  162.         }
  163.         /* 
  164.         ** 接收 CMPP 提交消息的应答
  165.         */
  166.         ret = SGIPGetSndAck( &sLid,   /*此值为发送短消息时,应用程序填写的序列号*/
  167.                     &ulOrgNodeNum, &ulOrgTimeStamp, &ulOrgSequence, 
  168.                     &iStatus);        /* 此条生消息发送到短消息中心时的状态,0表示成功,其它表示失败 */
  169. while( ret == SUCCESS )  /* 接收成功 */
  170. {
  171. printf("nrecv submit ack:LID = %d", LID);
  172.             printf("nNodeNum:%u, Time:%u, Sequence:%un", ulOrgNodeNum, ulOrgTimeStamp, ulOrgSequence);
  173. printf("nStatus:%dn", iStatus);
  174.             /* 看是否还有其这的应答消息 */
  175.             
  176. ret = SGIPGetSndAck( &sLid,
  177.         &ulOrgNodeNum, &ulOrgTimeStamp, &ulOrgSequence, 
  178.                     &iStatus);
  179. }        
  180.         /* 
  181.         ** 接收从短消息中心下发来的短消息 
  182.         */
  183.         memset(psUserNumber, 0, sizeof(psUserNumber));
  184.         memset(psSPNumber, 0, sizeof(psSPNumber));
  185.         memset(psMsgContent, 0, sizeof(psMsgContent));
  186.         memset(pReserve, 0, sizeof(pReserve));
  187.         //char sMsg[2048];
  188.         nLen = sizeof(sMsg);
  189.         ret = SGIPRcv(sMsg, &nLen);
  190.         while( ret == SUCCESS)
  191.         {
  192.             tagSgipHead* pHead = (tagSgipHead*)sMsg;
  193.             switch (ntohl(pHead->nCmdID))
  194.             {
  195.                 case SGIP_DELIVER:
  196.                     getdata_from_sgip_recvsm_frame(sMsg, nLen, psUserNumber, psSPNumber,
  197.                         &byTPPID, &byTPUDHI, &byMsgCoding, &nMsgLen,
  198.                         psMsgContent, pReserve);
  199.                     printf("nnRecv deliver sm Success!"); 
  200.                     printf("nUserNumber:%s", psUserNumber);
  201.                     printf("nSpNumber:%s", psSPNumber);
  202.                     printf("nTPPID:%d", byTPPID);
  203.                     printf("nTPUDHI:%d", byTPUDHI);
  204.                     printf("nMsgCoding:%d", byMsgCoding);
  205.                     printf("nMsgLen:%d", nMsgLen);
  206.                     printf("nReserve:%s", pReserve);
  207.                     
  208.                     printf("nMsgContent(txt):%s", psMsgContent);
  209.                     /* 
  210.                     ** 短消息内容的16进制显示 
  211.                     */
  212.                     printf("nSM Data Content(hex):");            
  213.                     tmpPsz = (unsigned char *)psMsgContent;
  214.                     while(nMsgLen > 0)
  215.                     {
  216.                         if(nMsgLen > 16)
  217.                             j = 16;
  218.                         else
  219.                             j = nMsgLen;
  220.                     
  221.                         for(i = 0; i< j; i++)
  222.                         {
  223.                             printf("%02x_", *(tmpPsz + i));
  224.                         }
  225.                         printf("rn");
  226.                         nMsgLen -= j;
  227.                         tmpPsz += 16;
  228.                     }                    
  229.                     break;                    
  230.                 case SGIP_REPORT:
  231.                     getdata_from_sgip_rptsm_frame(sMsg, nLen, &ulOrgNodeNum, 
  232.                         &ulOrgTimeStamp, &ulOrgSequence, &byReportType,
  233.                         psUserNumber, &bystate, &byErrCode, pReserve);
  234.                     printf("nnRecv report sm Success!"); 
  235.                     printf("nOrgNodeNum:%u", ulOrgNodeNum);
  236.                     printf("nOrgTimeStamp:%u", ulOrgTimeStamp);
  237.                     printf("nOrgSequence:%u", ulOrgSequence);                    
  238.                     printf("nReportType:%d", byReportType);
  239.                     printf("nUserNumber:%s", psUserNumber);
  240.                     printf("nState:%d", bystate);
  241.                     printf("nErrCode:%d", byErrCode);
  242.                     printf("nReserve:%s", pReserve);                    
  243.                     break;
  244.                 default:
  245.                     printf("nRecv unknown msg, command id:%d", pHead->nCmdID);
  246.                     break;                    
  247.             }
  248.             /* 
  249.             ** 看是否还有其它的消息 
  250.             */
  251.             memset(psUserNumber, 0, sizeof(psUserNumber));
  252.             memset(psSPNumber, 0, sizeof(psSPNumber));
  253.             memset(psMsgContent, 0, sizeof(psMsgContent));
  254.             memset(pReserve, 0, sizeof(pReserve));
  255.         
  256.             ret = SGIPRcv(sMsg, &nLen);
  257.         }
  258.         
  259.         
  260.         /* 等待3秒 */
  261.         sleep(1);
  262.         /*
  263.         i = getchar();
  264.         */
  265.     }
  266. }