ユーザアプリケーションプログラム作成例

メッセージ送受信の処理の流れを次の図に示し,その流れに沿ったコーディング例をC言語(K&R版)で示します。

図3-1 処理の流れ

[図データ]

注※
端末から受信したメッセージがブロードキャストメッセージまたはマルチキャストメッセージの場合は,メッセージ受信後の処理は不要です。

なお,TP1/NET/UDPでは,UAP1~UAP4に対応するC言語(K&R版)のコーディング例を次のファイルで提供しています。

メッセージ受信処理とユニキャストメッセージ送信(UAP1)
適用OSがLinuxの場合
  • /opt/OpenTP1/examples/mcf/UDPIP/aplib/c/ap1.c
その他のOSの場合
  • /BeTRAN/examples/mcf/UDPIP/aplib/c/ap1.c
ユニキャストメッセージ送信(UAP2)
適用OSがLinuxの場合
  • /opt/OpenTP1/examples/mcf/UDPIP/aplib/c/ap2.c
その他のOSの場合
  • /BeTRAN/examples/mcf/UDPIP/aplib/c/ap2.c
ブロードキャストメッセージ送信(UAP3)
適用OSがLinuxの場合
  • /opt/OpenTP1/examples/mcf/UDPIP/aplib/c/ap3.c
その他のOSの場合
  • /BeTRAN/examples/mcf/UDPIP/aplib/c/ap3.c
マルチキャストメッセージ送信(UAP4)
適用OSがLinuxの場合
  • /opt/OpenTP1/examples/mcf/UDPIP/aplib/c/ap4.c
その他のOSの場合
  • /BeTRAN/examples/mcf/UDPIP/aplib/c/ap4.c

メッセージ受信処理とユニキャストメッセージ送信(UAP1)

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <dcmcf.h>
#include <dcmudpu.h>

#define SEND_ADDR   0xC20B2A15   /* 0xC20B2A15 == 194.11.42.21 */
#define BRD_PORT    0x4E2A       /* 0x4E2A     == 20010        */
#define MLT_PORT    0x4E2B       /* 0x4E2B     == 20011        */
#define UNI_PORT    0x4E2C       /* 0x4E2C     == 20012        */
#define SRV_MSGSIZE    480
typedef struct srvmsg_s{
   char    mcfarea[8];             /*   8bytes == DCMCFBUF1   */
   dcmudp_dc_header dchead;        /*  24bytes                */
   char    srv_msg[SRV_MSGSIZE];   /* 480bytes == SRV_MSGSIZE */
} srvmsg_def;                       /* 512bytes                */

void    ex_uap1() {
   DCLONG          action;
   static DCLONG   commform   = DCNOFLAGS;
   char            termnam[16];
   static   char   resv01[16] = "¥0";
   static   char   resv02[16] = "¥0";
   srvmsg_def      srvdata_area;
   char            *recvdata;
   char            *senddata;
   DCLONG          sdataleng;
   DCLONG          rdataleng;
   DCLONG          inbufleng;
   static DCLONG   opcd       = DCNOFLAGS;
   static DCLONG   watchtime  = -1;
   DCLONG          time;
   int             rtn;

   recvdata = (char *)&srvdata_area;
   senddata = (char *)&srvdata_area;

   (void)memset((void *)&srvdata_area,
                0x00,
                sizeof(srvdata_area));

   action   = DCMCFFRST;

   inbufleng = sizeof(srvdata_area);

   rtn = dc_mcf_receive(action,
                        commform,
                        termnam,
                        resv01,
                        recvdata,
                        &rdataleng,
                        inbufleng,
                        &time);

   if( DCMCFRTN_00000 == rtn ) {
       if( SEND_ADDR == ntohl(srvdata_area.dchead.r_ipaddr) ) {
           switch( ntohs(srvdata_area.dchead.r_portno) ) {
           case BRD_PORT:
               /***********************************************/
               /*  User Service Part.                         */
               /***********************************************/
               break;

           case MLT_PORT:
               /***********************************************/
               /*  User Service Part.                         */
               /***********************************************/
               break;

           case UNI_PORT:
               /***********************************************/
               /*  User Service Part.                         */
               /***********************************************/
               action   = DCMCFEMI;
               sdataleng = rdataleng;
               rtn = dc_mcf_sendsync(action,
                                     commform,
                                     termnam,
                                     resv01,
                                     senddata,
                                     sdataleng,
                                     resv02,
                                     opcd,
                                     watchtime);

               if( DCMCFRTN_00000 == rtn) {
                   /*******************************************/
                   /*  User Service Part.                     */
                   /*******************************************/
               } else {
                   /*******************************************/
                   /*  User Service Part.                     */
                   /*******************************************/
               }
               break;

           default:
               /***********************************************/
               /*  User Service Part.                         */
               /***********************************************/
               break;
           }
       } else {
               /***********************************************/
               /*  User Service Part.                         */
               /***********************************************/
       }
   } else {
       /************************************************/
       /*  User Service Part.                          */
       /************************************************/
   }
   return;
}

ユニキャストメッセージ送信(UAP2)

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <dcmcf.h>
#include <dcmudpu.h>

#define SEND_ADDR   0xC20B2A16   /* 0xC20B2A16 == 194.11.42.22 */
#define SEND_PORT   0x4E2D       /* 0x4E2D     == 20013        */
#define SRV_MSGSIZE    480
typedef struct srvmsg_s{
   char    mcfarea[8];             /*   8bytes == DCMCFBUF1   */
   dcmudp_dc_header dchead;        /*  24bytes                */
   char    srv_msg[SRV_MSGSIZE];   /* 480bytes == SRV_MSGSIZE */
} srvmsg_def;                       /* 512bytes                */

void    ex_uap2() {
   static DCLONG   action     = DCMCFEMI;
   static DCLONG   commform   = DCNOFLAGS;
   static   char   *termnam   = "leid04";
   static   char   resv01[16] = "¥0";
   static   char   resv02[16] = "¥0";
   static DCLONG   opcd       = DCNOFLAGS;
   static DCLONG   watchtime  = -1;
   char            *senddata;
   srvmsg_def      senddata_area;
   DCLONG          sdataleng;
   int             rtn;

   senddata = (char *)&senddata_area;
   (void)memset((void *)&senddata_area,
                0x00,
                (size_t)sizeof(senddata_area));

   /************************************************/
   /*  User Service Part.                          */
   /************************************************/

   sdataleng = sizeof(senddata_area)
               - sizeof(senddata_area.mcfarea);

   senddata_area.dchead.r_ipaddr = htonl(SEND_ADDR);

   senddata_area.dchead.r_portno = htons(SEND_PORT);

   rtn = dc_mcf_sendsync(action,
                         commform,
                         termnam,
                         resv01,
                         senddata,
                         sdataleng,
                         resv02,
                         opcd,
                         watchtime);

   if( DCMCFRTN_00000 == rtn) {
       /************************************************/
       /*  User Service Part.                          */
       /************************************************/
   } else {
       /************************************************/
       /*  User Service Part.                          */
       /************************************************/
   }
   return;
}

ブロードキャストメッセージ送信(UAP3)

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <dcmcf.h>
#include <dcmudpu.h>

#define SEND_PORT      0x4E2E       /* 0x4E2E     == 20014     */
#define SRV_MSGSIZE    480
typedef struct srvmsg_s{
   char    mcfarea[8];             /*   8bytes == DCMCFBUF1   */
   dcmudp_dc_header dchead;        /*  24bytes                */
   char    srv_msg[SRV_MSGSIZE];   /* 480bytes == SRV_MSGSIZE */
} srvmsg_def;                       /* 512bytes                */

void    ex_uap3(){
   static DCLONG   action     = DCMCFEMI;
   static DCLONG   commform   = DCNOFLAGS;
   static   char   *termnam   = "leid05";
   static   char   resv01[16] = "¥0";
   static   char   resv02[16] = "¥0";
   static DCLONG   opcd       = DCNOFLAGS;
   static DCLONG   watchtime  = -1;
   char            *senddata;
   srvmsg_def      senddata_area;
   DCLONG          sdataleng;
   int             rtn;

   senddata = (char *)&senddata_area;
   (void)memset((void *)&senddata_area,
                0x00,
                sizeof(senddata_area));

   /************************************************/
   /*  User Service Part.                          */
   /************************************************/

   sdataleng = sizeof(senddata_area)
               - sizeof(senddata_area.mcfarea);

   senddata_area.dchead.r_ipaddr = htonl(INADDR_BROADCAST);

   senddata_area.dchead.r_portno = htons(SEND_PORT);

   rtn = dc_mcf_sendsync(action,
                         commform,
                         termnam,
                         resv01,
                         senddata,
                         sdataleng,
                         resv02,
                         opcd,
                         watchtime);
   if( DCMCFRTN_00000 == rtn) {
       /************************************************/
       /*  User Service Part.                          */
       /************************************************/
   } else {
       /************************************************/
       /*  User Service Part.                          */
       /************************************************/
   }
   return;
}

マルチキャストメッセージ送信(UAP4)

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <dcmcf.h>
#include <dcmudpu.h>

#define SEND_ADDR     0xE0010203   /* 0xE0010203 == 224.1.2.3  */
#define SEND_PORT     0x4E2F       /* 0x4E2F     == 20015      */
#define SRV_MSGSIZE   480
typedef struct srvmsg_s{
   char    mcfarea[8];             /*   8bytes == DCMCFBUF1   */
   dcmudp_dc_header dchead;        /*  24bytes                */
   char    srv_msg[SRV_MSGSIZE];   /* 480bytes == SRV_MSGSIZE */
} srvmsg_def;                       /* 512bytes                */

void    ex_uap4() {
   static DCLONG   action     = DCMCFEMI;
   static DCLONG   commform   = DCNOFLAGS;
   static   char   *termnam   = "leid06";
   static   char   resv01[16] = "¥0";
   static   char   resv02[16] = "¥0";
   static DCLONG   opcd       = DCNOFLAGS;
   static DCLONG   watchtime  = -1;
   char            *senddata;
   srvmsg_def      senddata_area;
   DCLONG          sdataleng;
   int             rtn;

   senddata = (char *)&senddata_area;
   (void)memset((void *)&senddata_area,
                0x00,
                (size_t)sizeof(senddata_area));

   /************************************************/
   /*  User Service Part.                          */
   /************************************************/

   sdataleng = sizeof(senddata_area)
               - sizeof(senddata_area.mcfarea);

   senddata_area.dchead.r_ipaddr = htonl(SEND_ADDR);

   senddata_area.dchead.r_portno = htons(SEND_PORT);

   rtn = dc_mcf_sendsync(action,
                         commform,
                         termnam,
                         resv01,
                         senddata,
                         sdataleng,
                         resv02,
                         opcd,
                         watchtime);

   if( DCMCFRTN_00000 == rtn) {
       /************************************************/
       /*  User Service Part.                          */
       /************************************************/
   } else {
       /************************************************/
       /*  User Service Part.                          */
       /************************************************/
   }
   return;
}