BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How can I know programatically my own IP?

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Internet Winsock
View previous topic :: View next topic  
Author Message
Juan Rosell
Guest





PostPosted: Thu Jan 15, 2004 3:34 pm    Post subject: How can I know programatically my own IP? Reply with quote




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





PostPosted: Thu Jan 15, 2004 6:54 pm    Post subject: Re: How can I know programatically my own IP? Reply with quote




"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





PostPosted: Mon Jan 26, 2004 3:38 pm    Post subject: Re: How can I know programatically my own IP? Reply with quote




"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
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Internet Winsock All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.