[fpc-pascal]getting my local ip

ron wilson ron.wilson at coastgames.com
Fri Mar 15 17:08:03 CET 2002

thanks - i tried this once before and i got this compiler error:

_revproxy.pp(37,29) Error: Array type required
_revproxy.pp(344) Fatal: There were 1 errors compiling module, stopping

here is my code.  (its under construction, but this at least should compile.

21 function getlocalip: ansistring;
22 var
23   pc: pchar;
24   ac: array[0..255] of char;
25   phe: phostent;
26   i: integer;
27 //  addr: in_addr;
28   s: ansistring;
29 begin
30   if (gethostname(ac,sizeof(ac))=SOCKET_ERROR) then halt;
31   writeln('host name is: ',ac);
32   phe:=gethostbyname(ac);
33   if phe=nil then halt;
34 //  writeln(sizeof(phe^.h_addr_list^));
36   i:=0;
37   while not(phe^.h_addr_list[i]=nil) do
38   begin
39     pc:=phe^.h_addr_list[i];
40     s:=strpas(pc);
41     writeln('address ',i,': ',s);
42     inc(i);
43   end;
44   getlocalip:=s;
45 end;

i just assumed i was doing something wrong with my syntax when i got that
error.  any suggestions?

ron wilson

-----Original Message-----
From: fpc-pascal-admin at deadlock.et.tudelft.nl
[mailto:fpc-pascal-admin at deadlock.et.tudelft.nl]On Behalf Of Michael Van
Sent: Friday, March 15, 2002 9:12 AM
To: fpc-pascal at deadlock.et.tudelft.nl
Subject: RE: [fpc-pascal]getting my local ip

On Fri, 15 Mar 2002, ron wilson wrote:

> ok - thats what i thought - so they both point to the same memory area.
> question is this then:  how do i access the list.  i guess my pascal
> is too far gone.  i understand pointers pretty well, but in this case, i'm
> lost.  what we have here is a pointer to pchar - ^pchar.  so to access the
> first pchar in the list i would code this:
> program test;
> var
>   pc: pchar;
>   h: hostent;
> begin
>   /* fill h here */
>   pc:=stralloc(sizeof(h.h_addr_list^));
>   strpcopy(pc,h.h_addr_list^);
>   writeln(pc);
> end.

No, no. You can use the fact that a pointer can be considered as a
pointer to an array.

While h.h_addr_list[i]<>Nil do
  PC:=h_addr_list[i];  // Get pchar pointer
  S:=StrPas(pc);       // Convert to pascal string.
  // Do something with S.

Have a look at the inet unit, the GetAlias method. It does exactly the
same for Linux.


fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org

More information about the fpc-pascal mailing list