Board index » delphi » Why doesn't this code work right?

Why doesn't this code work right?

I'm using the code below, and added winsock to my uses list.
I'm using D4 C/S.

Now when i do inttostr(my_ip_address) it returns some weird
numer -7723189217 bla bla (i random typed this one of course :-)

Why?

Rik

function my_ip_address:longint;
const
  bufsize=255;
var
  buf: pointer;
  RemoteHost : PHostEnt; (* No, don't free it! *)

begin
  buf:=NIL;
  try
    getmem(buf,bufsize);
    winsock.gethostname(buf,bufsize);   (* this one maybe without domain *)
    RemoteHost:=Winsock.GetHostByName(buf);
    if RemoteHost=NIL then
      my_ip_address:=winsock.htonl($07000001)  (* 127.0.0.1 *)
    else
      my_ip_address:=longint(pointer(RemoteHost^.h_addr_list^)^);
  finally
    if buf<>NIL then  freemem(buf,bufsize);
  end;
  result:=winsock.ntohl(result);
end;

 

Re:Why doesn't this code work right?


Have you done a WSAStartup before?

Alex

--
Author of the free Chatsystem PINO!
Available at http://pino.cjb.net

Re:Why doesn't this code work right?


Have you done a WSAStartup before?

Alex

--
Author of the free Chatsystem PINO!
Available at http://pino.cjb.net

Re:Why doesn't this code work right?


Try this.

function LocalIP : string;
type
    TaPInAddr = array [0..10] of PInAddr;
    PaPInAddr = ^TaPInAddr;
var
    phe  : PHostEnt;
    pptr : PaPInAddr;
    Buffer : array [0..63] of char;
    I    : Integer;
    GInitData      : TWSADATA;

begin
    WSAStartup($101, GInitData);
    Result := '';
    GetHostName(Buffer, SizeOf(Buffer));
    phe :=GetHostByName(buffer);
    if phe = nil then Exit;
    pptr := PaPInAddr(Phe^.h_addr_list);
    I := 0;
    while pptr^[I] <> nil do begin
      result:=StrPas(inet_ntoa(pptr^[I]^));
      Inc(I);
    end;
    WSACleanup;
end;

--
  Mark Zwicker
  Computer Programmer
  Adventus Inc.
  1-888-999-MIDI(6434)
  m...@adventus.com
  supp...@adventus.com

Other Threads