OpenTP1 Version 7 Programming Guide

[Contents][Index][Back][Next]

3.2.2 Coding examples for re-establishing or forcibly releasing a connection

This subsection provides coding examples for re-establishing or forcibly releasing a connection.

Organization of this subsection
(1) Coding example for re-establishing a connection
(2) Coding example for forcibly releasing a connection

(1) Coding example for re-establishing a connection

The figure and coding example below show an example of automatically re-establishing a connection after CERREVT (connection error) is reported.

Figure 3-3 UAP example for automatically re-establishing a connection

[Figure]

 
void cerrevt(){
    char            rcvdata[256];
    DCLONG          rcv_len;
    DCLONG          rtime;
    int             rtn;
    dcmcf_tactcnopt cnopt;
    dcmcf_tlscnopt  cnopt2;
    DCLONG  infcnt = 1;
    dcmcf_cninf     inf;
 
    rtn = dc_mcf_receive(DCMCFFRST, DCNOFLAGS, termnam, "",
                         rcvdata,&rcv_len, sizeof(rcvdata),
                         &rtime);
    if (DCMCFRTN_00000 == rtn){
 
    /* Processing during connection release          */
    /*                     :                         */
 
    /* Connection re-establishment request           */
        memset(&cnopt, 0, sizeof(cnopt));
        strcpy(cnopt.idnam, termnam);
 
        rtn = dc_mcf_tactcn(DCMCFLE, &cnopt, NULL, NULL,
                            NULL, NULL);
        if (DCMCFRTN_00000 == rtn){
    /* Acceptance of connection                      */
    /* re-establishment request: Successful          */
 
            while(1){
            /* Connection status acquisition */
                memset(&cnopt2, 0, sizeof(cnopt2);
                strcpy(cnopt2.idnam, termnam);
                memset(&inf, 0, sizeof(inf));
 
                rtn = dc_mcf_tlscn(DCMCFLE, &cnopt2, NULL, 
                                   NULL, NULL, &infcnt, 
                                   &inf, NULL);
                if (DCMCFRTN_00000 == rtn){
                    if (DCMCF_CNST_ACT == inf.status){
                        /* Connection established */
                        break;
                    }
                } else {
                    /* Error processing */
                }
                sleep(1);
            }
 
    /* Processing following connection establishment */
    /*                     :                         */
 
        } else {
    /* Acceptance of connection                      */
    /* re-establishment request: Failed              */
            /* Error processing */
        }
 
    } else {
        /* Error processing */
    }
   
    return;
}

(2) Coding example for forcibly releasing a connection

The figure and coding example below show an example of forcibly releasing a connection when the received message contains a format error.

Figure 3-4 UAP example for forcibly releasing a connection

[Figure]

 
void mhprecv(){
    char            rcvdata[256];
    DCLONG          rcv_len;
    DCLONG          rtime;
    int             rtn;
    int             check;
    dcmcf_tdctcnopt cnopt;
    dcmcf_tlscnopt  cnopt2;
    DCLONG  infcnt = 1;
    dcmcf_cninf     inf;
 
    rtn = dc_mcf_receive(DCMCFFRST, DCNOFLAGS, termnam, "",
                         rcvdata, &rcv_len, sizeof(rcvdata),
                         &rtime);
    if (DCMCFRTN_00000 == rtn){
 
        /* Checking of the received message */
        /*            :                */
 
        if (0 == check){
            /* Checking result: Valid */
            /* Processing when the result is normal */
        } else {
            /* Checking result: Invalid */
 
            /* Request to forcibly release the connection */
            memset(&cnopt, 0, sizeof(cnopt));
            strcpy(cnopt.idnam, termnam);
 
            rtn = dc_mcf_tdctcn(DCMCFLE | DCMCFFRC, &cnopt,
                                NULL, NULL, NULL, NULL);
            if (DCMCFRTN_00000 == rtn){
    /* Acceptance of forcible                        */
    /* connection release request: Successful        */
 
                while(1){
                    /* Connection status acquisition */
                    memset(&cnopt2, 0, sizeof(cnopt2);
                    strcpy(cnopt2.idnam, termnam);
                    memset(&inf, 0, sizeof(inf));
 
                    rtn = dc_mcf_tlscn(DCMCFLE, &cnopt2, NULL,
                                       NULL, NULL, &infcnt,
                                       &inf, NULL);
                    if (DCMCFRTN_00000 == rtn){
                        if (DCMCF_CNST_DCT == inf.status){
                            /* Connection released */
                            break;
                        }
                    } else {
                        /* Error processing */
                    }
                    sleep(1);
                }
 
    /* Processing following connection release       */
    /*                     :                         */
 
            } else {
     /* Acceptance of forcible                */
     /* connection release request: Failed    */
             /* Error processing */
            }
        }
 
    } else {
        /* Error processing */
    }
   
    return;
}