Operating System - OpenVMS
1753274 Members
5083 Online
108792 Solutions
New Discussion

Re: FORTRAN using TCPIP gethostbyname

 
Frank_Gribbin
Occasional Contributor

FORTRAN using TCPIP gethostbyname

Does anyone have an example of using host name lookup in FORTRAN.

I've tried to use QIO with IO$_ACPCONTROL but get access violations. 

I've also tried to call the C gethostbyname function, but the return code is always zero.

It seems this question was asked to the openvms wizard a long time ago.

http://h71000.www7.hp.com/wizard/wiz_7265.html

I have a FORTRAN program talking to the remote server (python on a raspbrry pi) based on the example

http://h71000.www7.hp.com/wizard/wiz_2777.html

However it's frustrating to have the ip address coded into the program. I'm using FORTRAN because the existing application is FORTRAN. 

 

 

 

 

12 REPLIES 12
abrsvc
Respected Contributor

Re: FORTRAN using TCPIP gethostbyname

Post the code that gets the ACCVIO.  Usually this happens because the parameters are not passed correctly.  It may be a simple fix to your existing code.

Dan

Steven Schweda
Honored Contributor

Re: FORTRAN using TCPIP gethostbyname

   It's sure been a long time since I did anything with Fortran.  I seem
to have forgotten most of it.  The following is crude, but might be
helpful.  (I assume that some of those modern POINTER things might look
less goofy than deref(), for example.)  The QIO method would probably be
smarter.

alp $ fort ghbn
alp $ link ghbn
alp $ run ghbn
addr = 216.237.214.019


> Post the code [...]

   For us non-psychics, it's tough to guess what you did wrong without
seeing what you did.  (If I can post the above embarrassment, then you
should be able to reveal yours.)

 

      The file ghbn.for does not have a valid extension for an
      attachment and has been removed. jpg,gif,png are the valid
      extensions.

   These forums get stupider by the day.  Your Web browser may hate
this non-JPEG ".jpg" file, but it should be willing to save it.
Otherwise, it should be possible to use Wget to fetch the file.

Hoff
Honored Contributor

Re: FORTRAN using TCPIP gethostbyname

The answer that the Wizard gave is still correct.  You are using OpenVMS, which means that there simply are not examples of every call in every language.    What was available of for programming examples for OpenVMS largely disappeared off the net a decade or more ago, unfortunately.   This means learning the OpenVMS calling standard and argument passing, and how that applies to this and other calls (call by value, call by reference, descriptor by address, etc), and how to use examples of one call for some other call.    Other example calls from usenet newsgroups such as comp.os,vms, or from the OpenVMS Freeware, or elsewhere.

Here, you can also translate an application-specific logical name — not my favorite approach — or can read the IP address from a data file, and bypass the problem. 

For this case, using the OpenVMS APIs?    Fortran isn't going to want to call gethostbyname() call directly either, as that call returns a pointer to a structure.    Fortran pointers differ somewhat from how C pointers work.   (Might also need to call the CRTL initialization routine decc$crtl_init call here too, if you're heading down that path and planning to use jackets and calls into the C RTL from an application with a main program written in a language other than C.)   Which means you're either going to be using $qio calls (e.g. see 2.16.2 for the general sequence, though that example is in C, and also see some of the old discussions from comp.os.vms newsgroup) to get the data from TCP/IP Services, or you're going to be using a wrapper around the C call to dereference and pass the data to the Fortran code.   (There are examples of the latter approach posted.)

FWIW, consider avoiding the IPv4-only gethostbyname() and gethostbyaddr() calls in any new code.   The getaddrinfo() and getnameinfo() calls are the preferred approach in new code.  (While OpenVMS does now have these two C calls and related baggage, there are wrinkles, and unfortunately there does not appear to be any documented $qio function code associated with these.)

If you're just starting to learn IP networking, there are available resources.

The comp.os.vms usenet newsgroup gets more traffic than this place, too.   To access that usenet newsgroup, you can either use Google Groups (a front end which has its own issues, but the search usually works) or scrounge a newsreader for your preferred local operating system and register for a free Usenet account from one of the various providers, such as Eternal September.

Francisco Hermida
Occasional Advisor

Re: FORTRAN using TCPIP gethostbyname

Thank you Steven Schweda!, 

I was always wondering how to deal with C functions returning pointers from FORTRAN. deref() might be rough, but it is simple and it works!.

Steven Schweda
Honored Contributor

Re: FORTRAN using TCPIP gethostbyname

> I was always wondering how to deal with C functions returning pointers
> from FORTRAN. deref() might be rough, but it is simple and it works!.

   Glad to help.  (It's been so long that I can't remember where I stole
that idea.  It seems unlikely that I invented it.)  Of course, "deref"
is really a misnomer, because the real trick is the %val() in the
caller.  deref() simply returns what it gets.

   It'd be nice if someone (perhaps someone who knows something about
pointers in Fortran) could explain how to deal with a C function like
getaddrinfo(), which returns pointers in arguments rather than as the
function value.  An alternative approach would be to write a
Fortran-friendly C wrapper for getaddrinfo(), but that would require a
Fortran user to have a C compiler, too.

Mike Kier
Valued Contributor

Re: FORTRAN using TCPIP gethostbyname

Steven, I cobbled up the start of a routine that performs the appropriate pointer stuff in Fortran (using the DEC-specific POINTER (pointer,pointee), not the Fortran 95 Pointer types.  It needs more work to turn it into a callable function, and I don't have the IPV6 bits implemented, but it may work as a jumping off point or just give you ideas for your own approach.

I'll try to attach it here.

Mike

example use:

$ mcr []F_GETADDRINFO localhost
[LOCALHOST]
AI_Flags = 0
AI_Family = INET
AI_SockType = SOCK_STREAM
AI_Protocol = IP_TCP
AI_CannonName = [LOCALHOST]
SIN_Len = 16
SIN_Family = INET
SIN_Port = 0
IPV4 Address = 127.0.0.1

AI_Flags = 0
AI_Family = INET
AI_SockType = SOCK_DGRAM
AI_Protocol = IP_UDP
AI_CannonName = [LOCALHOST]
SIN_Len = 16
SIN_Family = INET
SIN_Port = 0
IPV4 Address = 127.0.0.1

Practice Random Acts of VMS Marketing
Steven Schweda
Honored Contributor

Re: FORTRAN using TCPIP gethostbyname

> Steven, I cobbled up the start of a routine [...]

   Thanks for the helpful cobbling (including the better FORMAT).  My
more stone-age variation may be attached (depending on today's forum
vagaries).

alp $ fort gai
alp $ link gai
alp $ r gai
  getaddrinfo() = 0
  ai_flags = 0
  ai_family = 2
  ai_socktype = 1
  ai_protocol = 6
  ai_addrlen = 16
  ai_canonname_p = 0
  ai_addr_p = 2196368
  ai_next_p = 2390728
addr = 216.237.214.19

Steven Schweda
Honored Contributor

Re: FORTRAN using TCPIP gethostbyname

> Post the code [...]

   Still a good idea.

> I've also tried to call the C gethostbyname function, but the return
> code is always zero.

   I can do that, too, but with my weak psychic powers, I can't say that
your (invisible) code has the same defect as my demo.  Works:

alp $ fort ghbn_1
alp $ link ghbn_1
alp $ run ghbn_1
  decc$gethostbyname() = 270352

   Fails:

alp $ fort ghbn_2
alp $ link ghbn_2
alp $ run ghbn_2
  decc$gethostbyname() = 0

   The (working) program:

alp $ type ghbn_1.for
c     Example (lame) calling gethostbyname().

      program ghbn

      implicit none

      character* 256 name
      integer* 4 he_p

      integer* 4 decc$gethostbyname

c     NUL-terminate the name.
       name = 'ftp.hpe.com'// char(0)

c     Get the address of a hostent structure.
      he_p = decc$gethostbyname( %ref( name))

      type *, " decc$gethostbyname() = ", he_p

      end

   How to make it fail:

alp $ gdiff ghbn_1.for ghbn_2.for
5c5
<       implicit none
---
> ccc       implicit none
10c10
<       integer* 4 decc$gethostbyname
---
> ccc       integer* 4 decc$gethostbyname

    That is, failing to declare decc$gethostbyname as an integer is an
easy way to get an apparently bad result.  "implicit none" was, I claim,
the best thing ever to happen to FORTRAN/Fortran.

Steven Schweda
Honored Contributor

Re: FORTRAN using TCPIP gethostbyname

> I've tried to use QIO with IO$_ACPCONTROL but get access violations.

> Post the code [...]

   Still a good idea.

   Attached should be still more code in desperate need of refinement,
this time using $QIOW to do a GETHOSTBYNAME look-up.

alp $ fort qghbn
alp $ link qghbn
alp $ run qghbn
sys$assign() =    00000001
sys$qiow() =      00000001
sys$qiow( iosb) = 00000001
  addr_len =       4
addr = 216.237.214.19