Guild Companies, Inc.  
 
Midrange Programmer - How-To Advice & Free Code
OS/400 Edition
Volume 1, Number 1- January 17, 2002

Determining a PC's IP Address

by Shannon O'Donnell

To communicate with a PC from an RPG IV program, an important piece of information you'll need is the PC's IP. If you were only communicating with a single or a finite number of PCs, and those PCs always had the same IP address each time they logged onto your iSeries, you could store the IP address in a database file on your iSeries. That way you could just retrieve the local workstation ID for the iSeries job and look it up in the table of IP addresses, and you'd be all set. But that scenario is not likely to happen.

Any time a PC logs onto your iSeries via the Internet, it's almost guaranteed to have a different IP address each time. This is because IP addresses are limited in quantity, and are therefore valuable property. A person's Internet service provider needs to recycle IP addresses each time a PC connects to the Internet and then gets off again. This allows the ISP to reuse IP addresses, thereby cutting down on the number of IP addresses an ISP needs to have for a given set of users. And this same scenario is possible, and even likely, on a local area network where DHCP is used to provide dynamic IP addresses to PCs as they log on and off the local area network. The point is, you can't ever count on a PC having the same IP address every time it logs onto your system. Therefore, you need a way to programmatically determine the PC's IP address at runtime. With RPG IV and an iSeries API, accomplishing this goal is easy.

The sample program below demonstrates how to use the QDCRDEVD (Retrieve Device Description) API to retrieve a PC's IP address. The sample program shown here contains a prototype, a couple of C-specs, and a subprocedure, to demonstrate how to obtain the IP address of the PC running this sample.

Feel free to modify this code and play with it until you are comfortable with the way it works. In this issue of Midrange Programmer, you'll find the first of a series of articles from Ted Holt discussing prototypes and procedures. Since Ted will cover these concepts in much more detail, I won't explain the how's and why's of using them here. Rest assured, however, that you can copy and paste this code, as is, into any RPG IV program, or run it as shown here--it will work fine either way. And while you're doing that, we'll see what other handy little programs and procedures we can come up with to make your life as a programmer easier!

*==========================================================
* To Compile:
*
*    CRTBNDRPG  PGM(XXX/RTVIPADR1)
*
* Function: Retrieve IP address of PC
*
*==========================================================
H DFTACTGRP(*NO) BNDDIR('QC2LE')
D RtvIpAdr        PR            20a
D  Device                       10a   CONST
 *
D I_Net_Adr       S             16a
 *
D                SDS
D  Device               244    253
 *---------------------------------------------------------
 * Retrieve IP Address
C                   Eval      I_Net_Adr   = RtvIpAdr(Device)
C                   Eval      I_Net_Adr   = %trim(I_Net_Adr)

C     I_Net_Adr     Dsply
C                   Eval      *Inlr = *On
 *---------------------------------------------------------
 *    RtvIpAdr - Subprocedure To Retrieve PC's IP Address
 *---------------------------------------------------------
P RtvIpAdr        B                   Export
D RtvIpAdr        PI            20A
D  Inp_Device                   10A   Const
D Apierr          DS
D  Bytprv                 1      4B 0 Inz(216)
D  Bytavl                 5      8B 0 Inz
D  Errid                  9     15A   Inz
D  Rsvd                  16     16A   Inz
D  Errdta                17    216A   Inz
D Net_Address     S             20A   INZ
D Format          S              8A   Inz('DEVD0600')
D Rcvar           S           5000A   Inz
D Varlen          S              4B 0 Inz(5000)
C                   Eval      Device = Inp_Device
C                   Call      'QDCRDEVD'
C                   Parm                    Rcvar
C                   Parm                    Varlen
C                   Parm                    Format
C                   Parm                    Device
C                   Parm                    Apierr
C                   If        BytAvl = 0
C                   Eval      Net_Address = %Subst(Rcvar:877:16)
C                   Endif
C                   Return    Net_Address
P RtvIpAdr        E

Sponsored By
SOFTLANDING SYSTEMS

Thinking HIGH AVAILABILITY?

Think SOFTWARE MANAGEMENT First!

80% of unplanned downtime is caused by Application Failure or Operator Error, not hardware failure, according to IBM’s iSeries 400 Availability Team.

Software Management is essential to keeping your applications available, reliable, and bug-free, no matter how often you update them. Let SoftLanding show you how. You'll finish software projects faster, with a higher degree of quality, and keep them online, using our industry-leading solutions for CHANGE MANAGEMENT, DEBUGGING, TESTING, DEPLOYMENT, DATABASE REORGS, and PROBLEM DIAGNOSIS & RESOLUTION.

High Availability through Software Management. For more info and FREE downloads, visit http://www.softlanding.com/products/400 or email info@softlanding.com.

THIS ISSUE
SPONSORED BY:
Help/Systems
SoftLanding Systems
BCD Software Int'l
Jacada Ltd.
RJS Software Systems
WorksRight Software, Inc.
BACK ISSUES
TABLE OF CONTENTS
Welcome to Midrange Programmer, OS/400 Edition
Determining a PC's IP Address
I Lost My License Key!
Calling a Program Using the iSeries Toolbox for Java
RPG Prototyping
JavaServer Pages 101
  Newsletters | Subscribe | Advertise | About Us | Contact | Search | Home  
  Last Updated: 1/14/02
Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved.