 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Juan Rosell Guest
|
Posted: Thu Jan 15, 2004 3:34 pm Post subject: How can I know programatically my own IP? |
|
|
Does someone know how can I know programatically the IP of a machine where
an application is running?
Regards,
Juan!
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Thu Jan 15, 2004 6:54 pm Post subject: Re: How can I know programatically my own IP? |
|
|
"Juan Rosell" <juan (AT) eines (DOT) es> wrote
| Quote: | Does someone know how can I know programatically the
IP of a machine where an application is running?
|
Look at WinSock's gethostname() and gethostbyname() functions. The hostent
structure that gethostbyname() returns will contain all of the IPs for the
machine (a machine can have multiple IPs if it has multiple network adapters
installed).
Gambit
|
|
| Back to top |
|
 |
Jason Guest
|
Posted: Mon Jan 26, 2004 3:38 pm Post subject: Re: How can I know programatically my own IP? |
|
|
"Juan Rosell" <juan (AT) eines (DOT) es> wrote
| Quote: |
Does someone know how can I know programatically the IP of a machine where
an application is running?
Regards,
Juan!
|
Poorly written sample
function GetIPFromHost
(var HostName, IPaddr, WSAErr: string): Boolean;
type
Name = array[0..100] of Char;
PName = ^Name;
var
HEnt: pHostEnt;
HName: PName;
WSAData: TWSAData;
i: Integer;
begin
Result := False;
if WSAStartup($0101, WSAData) <> 0 then begin
WSAErr := 'Winsock is not responding."';
Exit;
end;
IPaddr := '';
New(HName);
if GetHostName(HName^, SizeOf(Name)) = 0 then
begin
HostName := StrPas(HName^);
HEnt := GetHostByName(HName^);
for i := 0 to HEnt^.h_length - 1 do
IPaddr :=
Concat(IPaddr,
IntToStr(Ord(HEnt^.h_addr_list^[i])) + '.');
SetLength(IPaddr, Length(IPaddr) - 1);
Result := True;
end
else begin
case WSAGetLastError of
WSANOTINITIALISED:WSAErr:='WSANotInitialised';
WSAENETDOWN :WSAErr:='WSAENetDown';
WSAEINPROGRESS :WSAErr:='WSAEInProgress';
end;
end;
Dispose(HName);
WSACleanup;
end;
procedure GetTheIP
begin
if GetIPFromHost(Host, IP, Err) then begin
//Label2.Caption := Host;
Label3.Caption := IP;
end
else
MessageDlg(Err, mtError, [mbOk], 0);
end;
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|