Delphi로 IP 알아오기

전에 도메인 명으로 IP를 알아오기와 비슷한 내용입니다.

Uses WinSock;
...
function My_LocalIP : String;
var
  wData: WSADATA;
  HostName: String;
  pHostInfo: pHostEnt;
begin
  WSAStartup(MAKEWORD(2, 2), wData);

  GetHostName(PAnsiChar(HostName), 512);
  pHostInfo := GetHostByName(PAnsiChar(Hos
tName));
  if Assigned(pHostInfo) then
    Result := IntToStr(ord(pHostInfo.h_addr_list^[0]))
      + '.' + IntToStr(ord(pHostInfo.h_addr_list^[1])) + '.' + IntToStr
      (ord(pHostInfo.h_addr_list^[2])) + '.' + IntToStr
      (ord(pHostInfo.h_addr_list^[3]))
  else
    Result := '';
  WSACleanup;
end;

HostName에 값이 없는 경우 로컬IP의 내용이 나타납니다.
Uses 문에 Winsock을 추가해 주어야 합니다.

실제 IP를 읽어 오는 내용입니다.

function My_RealIP : String;
var
  IdHTTP: TIdHTTP;
  st: integer;
begin
  Result := ''; // 못구했을땐 아무값도 없음

  IdHTTP := TIdHTTP.Create(nil);
  try
    Result := IdHTTP.Get('http://checkip.dyndns.com');
    st := pos(': ', Result);
    Result := Copy(Result, st + 1, MaxInt);
    st := pos('<', Result);
    Result := Copy(Result, 1, st - 1);

  finally
    IdHTTP.free;
  end;
end;

웹으로 내오는 내용을 짤라서 값을 표현합니다.

Author: yyjksw