× 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: Re: How to retrieve IP address
  • From: "Scott Klement" <infosys@xxxxxxxxxxxx>
  • Date: 10 Aug 1999 10:38:41 -0500

Hmmm... maybe you meant to use gethostbyname instead of gethostid?
I can't imagine a practical use for the gethostid() function in
an RPG program.    The results of gethostid() are just telling you
what has been set with sethostid() for the local machine only.
I assume that this is just included for compatability with unix,
as I can't think of an instance where it'd be useful on the AS/400.
(heck, its even deprecated on my unix box)

You know, if you truly want help with your program, its appropriate
to post the source code.   Otherwise, theres very little that we
can do to help you!!

I have a standard subprocedure out of a service program that I use
to resolve BOTH domain names and human-readable IP addresses into
the "long integer" format that the APIs need...   You can either
use it to get your IP addresses, or you can use it as an example
of how to make your own routine work.   I'll attach it at the
bottom of this message.

Scott Klement
Information Systems Manager
Klement's Sausage Co, Inc.


"A.Vishnu Vardhan" <vishvar@yahoo.com> wrote:
>
> I am working on a socket program in ILE RPG. In the client program I
>  am
> trying to retrieve the hostID.  I am using Eval = GetHostId.
> But it's returning only zeros. Any help. Instead if I want to hard
>  code
> it. How to specify my IP 164.164.18.5
> in Hex format. Thanks in advance.
>
> Vishnu Vardhan A
> BFL Software Limited.


d* this prototype usually goes in a /COPY member:
D ResolveIP       PR            10I 0 opdesc
D   Host                       256A   Const Options(*varsize)
D   IP                          10U 0


D* this is an example of how to use the subprocedure:
D IP_Addr         S             10U 0

C                   if        ResolveIP('www.midrange.com':IP_Addr)<0
c                   eval      Msg = 'Lookup Failed!'
c                   dsply                   Msg
c                   eval      *INLR = *On
c                   Return
c                   endif

C* Assuming the program didn't end, the value of IP_Addr is a valid
C* IP Address to use in connect(), etc.

c                   if        ResolveIP('209.100.31.100':IP_Addr)<0
c                   eval      Msg = 'Invalid IP Address!'
c                   dsply                   Msg              50
c                   eval      *INLR = *On
c                   Return
c                   endif

C* Assuming the program didn't end, the value of IP_Addr is a valid
C* IP Address to use in connect(), etc.


P* this is the actual subprocedure that does the work:
P*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
P**  Resolve to IP Address...
P**    Converts a host name from either dotted decimal format or
P**    from a domain name and gets the proper IP address for it.
P**
P**  Input:      peHost -- host name in DNS or dotted-decimal format
P**  Output:     peIP -- IP address (unsigned integer)
P**  Returns:    0 = success, negative value upon failure.
P**
P**  DNS (Domain name service) format is like: "mycomputer.myhost.com"
P**  Dotted-Decimal is like: 192.168.5.124
P*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
P ResolveIP       B                   EXPORT
D ResolveIP       PI            10I 0 opdesc
D   peHost                     256A   Const Options(*varsize)
D   peIP                        10U 0

D GetHostNam      PR              *   extProc('gethostbyname')
D  HostName                    256A

D p_hostent       S               *
D hostent         DS                  Based(p_hostent)
D   h_name                        *
D   h_aliases                     *
D   h_addrtype                   5I 0
D   h_length                     5I 0
D   h_addrlist                    *
D p_h_addr        S               *   Based(h_addrlist)
D h_addr          S             10U 0 Based(p_h_addr)

D inet_addr       PR            10U 0 ExtProc('inet_addr')
D  char_addr                    16A

D wkDotted        S             16A
D wkHostName      S            257A
D INADDR_NON      C                   CONST(4294967295)
D wkIP            S             10U 0
D wkParmNo        S             10I 0
D wkDataType      S             10I 0
D wkCurrLen       S             10I 0
D wkMaxLen        S             10I 0

c* get length of string
c                   callb     'CEEGSI'
c                   parm      1             wkParmNo
c                   parm                    wkDataType
c                   parm                    wkCurrLen
c                   parm                    wkMaxLen

c                   eval      wkHostName = %subst(peHost:1:wkCurrLen)
c                   eval      wkHostName = %trim(wkHostName) + x'00'
c                   eval      wkDotted =%trim(%subst(wkHostName:1:15))
c                             x'00'

C* first try to convert from dotted decimal format:
c                   eval      wkIP = inet_addr(wkDotted)

C* if that fails, try to do a DNS lookup
c                   if        wkIP = INADDR_NON

c                   eval      p_hostent = gethostnam(wkHostName)

C* if DNS lookup failed, its not a valid host.
c                   if        p_hostent = *NULL
c                   return    -1
c                   endif

c                   eval      wkIP = h_addr

c                   endif

c                   eval      peIP = wkIP
c                   return    0
P                 E
+---
| This is the RPG/400 Mailing List!
| To submit a new message, send your mail to RPG400-L@midrange.com.
| To subscribe to this list send email to RPG400-L-SUB@midrange.com.
| To unsubscribe from this list send email to RPG400-L-UNSUB@midrange.com.
| Questions should be directed to the list owner/operator: david@midrange.com
+---END



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.