[fpc-pascal]Local Ip Address

Jeremy Cowgar jc at cowgar.com
Mon Feb 23 16:02:53 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.
>  
>
Here's my code that I use:

uses sockets, errors, inet;

{ ... functions cut not important to your question ... }

function Open(host : string; port : integer) : integer;
var
    h : THost;
    addr : TInetSockAddr;
begin
    h.NameLookup(host);
    if h.LastError <> 0 then
      begin
        Open := 1;
        Exit;
      end;  

    addr.family := AF_INET;
    addr.port   := ShortHostToNet(port);
    addr.addr   := HostTonet(longint(h.IPAddress));
   
    sock := Socket(AF_INET, SOCK_STREAM, 0);
    if Not Connect(sock, addr, sin, sout) then
      begin
        Open := 2;
        Exit;
      end;
   
    rewrite(sout);
    reset(sin);
   
    Open := 0;
end;

Hope this helps.

Jeremy





More information about the fpc-pascal mailing list