[fpc-pascal]Local Ip Address

Michalis Kamburelis michalis at camelot.homedns.org
Mon Feb 23 16:45:11 CET 2004


Crabtree, Chad wrote:
> Hello I am having a dikens of a time getting my IP address on the machine
> that I am working on.  I've looked in the mailing list and I found something
> that I've been toying with but still I cannot get the xxx.xxx.xxx.xxx style
> name I can only get my machine name.  Here's what I have which I can get the
> names but not the byte code address.
> I know how to do this in python gethostbyname(gethostname()) by I'm having a
> hard time with this.
> program get_ip;
> uses sockets,winsock;
> const
> 	len:integer=256;
> var 
> 	lsock,y,z:longint;
> 	ac: array[0..255] of char;
> 	phe:phostent;
> 	xs:pchar;
> 	Adder:TInetSockAddr;
> 
> 
> begin 
> 	writeln(gethostname(ac,len));
> 	writeln(ac);
> 	phe:=gethostbyname(ac);
> 	xs:=phe^.h_addr_list[1];
> 	writeln(xs);
> 	phe:=gethostbyaddr(ac,4,PF_INET);
> 	xs:=phe^.h_name;
> 	writeln(xs)
> 	
> end.
> 	
I'm attaching some quick&dirty hacks of your code that make everything 
working. It will also work on Linux (and other UNIXes, probably) when 
you replace winsock with Libc (in fact, I was doing my fixes while 
sitting on Linux... but I just checked this on Windows and it works). 
Note that it may return as address '127.0.0.1' which may or may not be 
usable to you.

-- 
Michalis
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: self_addr.dpr
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20040223/4166ac21/attachment.ksh>


More information about the fpc-pascal mailing list