× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.


  • Subject: One more example for today !
  • From: "Phil Hall" <hallp@xxxxxxxx>
  • Date: Tue, 13 Jun 2000 16:58:29 -0500

Here's a little example of using the Dynamic Screen Manager (DSM) API's. It
requests a user supplied number of ephemeral ports from OS/400 - I used it
to test a error when the OS returned negative port numbers on a old release,
but what it does, doesn't really matter it's real use now is for showing the
DSM API's

Create the program as PORTGRAB, then call it. If you call it with zero
parameters, a usage message is displayed.

Using DSM is real fun !

[Sorry if the long lines are split]

--phil

/*-------------------------------------------------
 * includes
 *------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <netdb.h>

#include <qsnapi.h>

/*-------------------------------------------------
 * defines
 *------------------------------------------------*/
#define TRUE   1
#define FALSE  1

/*-------------------------------------------------
 * prototypes
 *------------------------------------------------*/
void DrawBasicWindow(const Qsn_Cmd_Buf_T *cmdBuffer, const Qsn_Win_T
*window);
void DrawWindow(const Qsn_Win_T *window, const Qsn_Cmd_Buf_T *cmdBuffer);
void DisplayUsageData(Qsn_Win_T *window);

/*-------------------------------------------------
 * structures
 *------------------------------------------------*/
typedef struct storage
{
    Qsn_Ssn_Desc_T sessDesc;
    char           buffer[100];
} storage;

/*-------------------------------------------------
 * global/static
 *------------------------------------------------*/

/*-------------------------------------------------
 * main()
 *------------------------------------------------*/
int main(int argc, char **argv)
{
    Qsn_Win_Ext_Inf_T extWinInf = { NULL, NULL, NULL, NULL, NULL, NULL };
    Qsn_Win_Desc_T winDesc;
    Qsn_Win_T      theWindow;
    Q_Bin4         winDescLen = sizeof(winDesc);
    Qsn_Cmd_Buf_T  cmdBuffer;
    char           textBuffer[60] = "";
    char           aid;

    long           counter = 0;
    long           tries = 1;

    /*-- set up session window --*/
    QsnInzWinD(&winDesc, winDescLen, NULL);
    winDesc.GUI_support = '0';
    winDesc.top_row = 3;
    winDesc.left_col = 10;
    winDesc.num_rows = 12;
    winDesc.num_cols = 60;
    extWinInf.draw_fp = DrawWindow;

    theWindow = QsnCrtWin( &winDesc,
         winDescLen,
         &extWinInf,
         sizeof(extWinInf),
         '1',
         NULL, 0, NULL, NULL);

    /*-- sanity check for parameters --*/
    switch ( argc )
    {
        case 2:
        {
            tries = atoi( argv[1] );
            if ( tries < 1 )
            {
                DisplayUsageData(&theWindow);
                exit( 99 );
            }
            break;
        }

        default:
        {
            DisplayUsageData(&theWindow);
            exit( 99 );
        }
    }

    cmdBuffer = QsnCrtCmdBuf(100, 20, 0, NULL, NULL);
    sprintf(textBuffer, "TCP/IP Port Grabber started...");
    QsnWrtDta(textBuffer, strlen(textBuffer), 0, 3, 1, QSN_SA_HI,
QSN_SA_NORM,
               QSN_SA_NORM, QSN_SA_NORM, cmdBuffer, theWindow, NULL);
    sprintf(textBuffer, "Settings: iterations = %ld", tries);
    QsnWrtDta(textBuffer, strlen(textBuffer), 0, 5, 3, QSN_SA_HI,
QSN_SA_NORM,
               QSN_SA_NORM, QSN_SA_NORM, cmdBuffer, theWindow, NULL);
    QsnPutBuf(cmdBuffer, 0, 0);

    for ( counter = 0; counter < tries; counter++)
    {
        unsigned short thePort = 0;
        int            theSock = 0;
        int            struct_size = 0;
        struct         sockaddr_in addr, serv_addr;

        if ( (theSock = socket(AF_INET, SOCK_STREAM, 0)) <0 )
        {
            sprintf(textBuffer, "Unable to create socket");
            QsnWrtDta(textBuffer, strlen(textBuffer), 0, 7, 3, QSN_SA_HI,
QSN_SA_NORM,
                      QSN_SA_NORM, QSN_SA_NORM, cmdBuffer, theWindow, NULL);
            sprintf(textBuffer, "TCP/IP Port Grabber ended.");
            QsnWrtDta(textBuffer, strlen(textBuffer), 0, 9, 1, QSN_SA_HI,
QSN_SA_NORM,
                       QSN_SA_NORM, QSN_SA_NORM, cmdBuffer, theWindow,
NULL);
            QsnPutBuf(cmdBuffer, 0, 0);
            aid = QsnGetAID(NULL, 0, NULL);
            return -1;
        }

        memset(&serv_addr, 0, sizeof(serv_addr) );
        serv_addr.sin_family = AF_INET;
        serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
        serv_addr.sin_port = htons(thePort);

        if ( bind( theSock, (struct sockaddr *)&serv_addr,
sizeof(serv_addr)) < 0)
        {
            struct_size = sizeof(serv_addr);
            getsockname(theSock, (struct sockaddr *) &serv_addr,
&struct_size);
            thePort = ntohs(serv_addr.sin_port);
            close(theSock);
            sprintf(textBuffer, "Unable to bind to socket number %d, on port
%d", theSock, thePort);
            QsnWrtDta(textBuffer, strlen(textBuffer), 0, 7, 3, QSN_SA_HI,
QSN_SA_NORM,
                      QSN_SA_NORM, QSN_SA_NORM, cmdBuffer, theWindow, NULL);
            sprintf(textBuffer, "TCP/IP Port Grabber ended.");
            QsnWrtDta(textBuffer, strlen(textBuffer), 0, 9, 1, QSN_SA_HI,
QSN_SA_NORM,
                       QSN_SA_NORM, QSN_SA_NORM, cmdBuffer, theWindow,
NULL);
            QsnPutBuf(cmdBuffer, 0, 0);
            aid = QsnGetAID(NULL, 0, NULL);
            return -1;
        }

        struct_size = sizeof(serv_addr);
        getsockname(theSock, (struct sockaddr *) &serv_addr, &struct_size);
        thePort = ntohs(serv_addr.sin_port);

        if ( thePort < 0 )
        {
            close(theSock);
            sprintf(textBuffer, "Negative port returned by OS/400, after %ld
iterations", counter);
            QsnWrtDta(textBuffer, strlen(textBuffer), 0, 7, 3, QSN_SA_HI,
QSN_SA_NORM,
                      QSN_SA_NORM, QSN_SA_NORM, cmdBuffer, theWindow, NULL);
            sprintf(textBuffer, "TCP/IP Port Grabber ended.");
            QsnWrtDta(textBuffer, strlen(textBuffer), 0, 9, 1, QSN_SA_HI,
QSN_SA_NORM,
                       QSN_SA_NORM, QSN_SA_NORM, cmdBuffer, theWindow,
NULL);
            QsnPutBuf(cmdBuffer, 0, 0);
            aid = QsnGetAID(NULL, 0, NULL);
            return -1;
        }

        /*-- finished with the socket --*/
        close( theSock );
    }

    /*-- finished our test --*/
    sprintf(textBuffer, "OS/400 successfully returned %ld port numbers.",
counter);
    QsnWrtDta(textBuffer, strlen(textBuffer), 0, 7, 3, QSN_SA_HI,
QSN_SA_NORM,
              QSN_SA_NORM, QSN_SA_NORM, cmdBuffer, theWindow, NULL);
    sprintf(textBuffer, "TCP/IP Port Grabber ended.");
    QsnWrtDta(textBuffer, strlen(textBuffer), 0, 9, 1, QSN_SA_HI,
QSN_SA_NORM,
               QSN_SA_NORM, QSN_SA_NORM, cmdBuffer, theWindow, NULL);
    QsnPutBuf(cmdBuffer, 0, 0);
    aid = QsnGetAID(NULL, 0, NULL);
    return 0;
}

void DrawBasicWindow(const Qsn_Cmd_Buf_T *cmdBuffer, const Qsn_Win_T
*window)
{
    char           *f_keys = "F3=Exit";
    char           *title  = "TCP/IP Port Grabber";

    QsnWrtDta( title, strlen(title), 0, 1, 20, QSN_SA_HI, QSN_SA_NORM,
               QSN_SA_WHT, QSN_SA_NORM, *cmdBuffer, *window, NULL);
    QsnWrtDta( f_keys, strlen(f_keys), 0, -1, 1, QSN_SA_HI, QSN_SA_NORM,
               QSN_SA_BLU, QSN_SA_NORM, *cmdBuffer, *window, NULL);
    return;
}

void DrawWindow(const Qsn_Win_T *window, const Qsn_Cmd_Buf_T *cmdBuffer)
{
    DrawBasicWindow(cmdBuffer, window);
    return;
}

void DisplayUsageData(Qsn_Win_T *window)
{
    char aid;
    Qsn_Cmd_Buf_T cmdBuffer;
    char *m1 = "Usage PORTGRAB 'iterations'";
    char *m2 = "Where:";
    char *m3 = "  'iterations' is the number of times to request";
    char *m4 = "               OS/400 to generate a port number.";
    char *m5 = "Example: PORTGRAB '500'";

    cmdBuffer = QsnCrtCmdBuf(100, 20, 0, NULL, NULL);
    QsnWrtDta( m1, strlen(m1), 0, 3, 2, QSN_SA_HI, QSN_SA_NORM,
               QSN_SA_NORM, QSN_SA_NORM, cmdBuffer, *window, NULL);
    QsnWrtDta( m2, strlen(m2), 0, 5, 2, QSN_SA_HI, QSN_SA_NORM,
               QSN_SA_NORM, QSN_SA_NORM, cmdBuffer, *window, NULL);
    QsnWrtDta( m3, strlen(m3), 0, 6, 2, QSN_SA_HI, QSN_SA_NORM,
               QSN_SA_NORM, QSN_SA_NORM, cmdBuffer, *window, NULL);
    QsnWrtDta( m4, strlen(m4), 0, 7, 2, QSN_SA_HI, QSN_SA_NORM,
               QSN_SA_NORM, QSN_SA_NORM, cmdBuffer, *window, NULL);
    QsnWrtDta( m5, strlen(m5), 0, 9, 2, QSN_SA_HI, QSN_SA_NORM,
               QSN_SA_NORM, QSN_SA_NORM, cmdBuffer, *window, NULL);

    QsnPutBuf(cmdBuffer, 0, 0);
    aid = QsnGetAID(NULL, 0, NULL);
    return;
}


+---
| This is the C/400 Mailing List!
| To submit a new message, send your mail to C400-L@midrange.com.
| To subscribe to this list send email to C400-L-SUB@midrange.com.
| To unsubscribe from this list send email to C400-L-UNSUB@midrange.com.
| Questions should be directed to the list owner/operator: bob@cstoneindy.com
+---


As an Amazon Associate we earn from qualifying purchases.

This thread ...


Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.