|MY IP WITH DELPHI|
Internet this and Internet that. Everybody wants to be on the Internet nowadays. Everyone wants to program Internet nowadays.
IP? TCP? uses Winsock Get IP Delphi.Project.Code
One of the most interesting tasks when starting to code for the Internet is how to obtain the IP address of a computer connected to the Internet.
Simply technical: the Internet is built on TCP/IP connections. The TCP part describes how two computers set up a connection to each other and transfer data. IP part primarily deals with how to get a message routed across the Internet. Each connected machine has a unique IP address that allows others to figure out a path to any computer around the WWW (or the world precisely).
To obtain the IP address of the computer you are using when connected to the Internet we need to call some of the API functions *defined* in the Winsock unit.
We'll create a GetIPFromHost function that calls several Winsock API functions in order to get the IP. Before we can even use WinSock functions, we must have a valid session. This session is created with the WinSock WSAStartup function. At the end of our function a call to SACleanup is made in order to terminate the use of the Windows Sockets API's. To obtain the computer's IP address, we must use GetHostByName in conjunction with GetHostName. Each computer is called a host and we can get the hostname with a special function call: GetHostName. We than use GetHostByName to get the IP-address, related to this hostname.
Start Delphi and place one Button and two Edit boxes on a newly created Form. Add the GetIPFromHost function to the implementation part of your unit and assign the following code to the OnClick event handler of a button (below):
(var HostName, IPaddr, WSAErr: string): Boolean;
Name = array[0..100] of Char;
PName = ^Name;
Result := False;
if WSAStartup($0101, WSAData) <> 0 then begin
WSAErr := 'Winsock is not responding."';
IPaddr := '';
if GetHostName(HName^, SizeOf(Name)) = 0 then
HostName := StrPas(HName^);
HEnt := GetHostByName(HName^);
for i := 0 to HEnt^.h_length - 1 do
IntToStr(Ord(HEnt^.h_addr_list^[i])) + '.');
SetLength(IPaddr, Length(IPaddr) - 1);
Result := True;
case WSAGetLastError of
Get IP Delphi.Project.Code