www.주소를 IP알아내고 Ping 날려보기…

말그대로 입니다.
물론 인디를 써도 됩니다. 근데. 웬지 말들고 싶어서 짜집기 해서 맹글어 봤습니다.
출처는 이곳 저곳에서 짜집기 한것으로 꽤 오래전에 만든거라.. ㅜ.ㅜ  죄송 합니다.

거두 절미하고 바로 소스 나갑니다.  참고 되시기 바랍니다.
델마당의 경우 Ping(WWW2IP(‘www.delmadang.com’),1000)  로 하면.. 약 0.094~0.15 초정도..

Uses 문에 Winsock을 포함하시고요.

Const
  // 선언하시고요..
  IP_STATUS_BASE           = 11000;
  IP_SUCCESS               = 0;
  IP_BUF_TOO_SMALL         = IP_STATUS_BASE + 1;
  IP_DEST_NET_UNREACHABLE  = IP_STATUS_BASE + 2;
  IP_DEST_HOST_UNREACHABLE = IP_STATUS_BASE + 3;
  IP_DEST_PROT_UNREACHABLE = IP_STATUS_BASE + 4;
  IP_DEST_PORT_UNREACHABLE = IP_STATUS_BASE + 5;
  IP_NO_RESOURCES          = IP_STATUS_BASE + 6;
  IP_BAD_OPTION            = IP_STATUS_BASE + 7;
  IP_HW_ERROR              = IP_STATUS_BASE + 8;
  IP_PACKET_TOO_BIG        = IP_STATUS_BASE + 9;
  IP_REQ_TIMED_OUT         = IP_STATUS_BASE + 10;
  IP_BAD_REQ               = IP_STATUS_BASE + 11;
  IP_BAD_ROUTE             = IP_STATUS_BASE + 12;
  IP_TTL_EXPIRED_TRANSIT   = IP_STATUS_BASE + 13;
  IP_TTL_EXPIRED_REASSEM   = IP_STATUS_BASE + 14;
  IP_PARAM_PROBLEM         = IP_STATUS_BASE + 15;
  IP_SOURCE_QUENCH         = IP_STATUS_BASE + 16;
  IP_OPTION_TOO_BIG        = IP_STATUS_BASE + 17;
  IP_BAD_DESTINATION       = IP_STATUS_BASE + 18;
  IP_ADDR_DELETED          = IP_STATUS_BASE + 19;
  IP_SPEC_MTU_CHANGE       = IP_STATUS_BASE + 20;
  IP_MTU_CHANGE            = IP_STATUS_BASE + 21;
  IP_UNLOAD                = IP_STATUS_BASE + 22;
  IP_GENERAL_FAILURE       = IP_STATUS_BASE + 50;
  MAX_IP_STATUS            = IP_GENERAL_FAILURE;
  IP_PENDING               = IP_STATUS_BASE + 255;

Type
  IPINFO = Record
    Ttl: Char;
    // Time To Live
    Tos: Char;
    // Type Of Service
    IPFlags: Char;
    // IP flags
    OptSize: Char;
    // Size of options data
    Options: pChar;
    // Options data buffer
  end;

  ICMPECHO = Record
    Source: ULONG;
    // Source address
    Status: ULONG;
    // IP status
    RTTime: ULONG;
    // Round trip time in milliseconds
    DataSize: SHORT;
    // Reply data size
    Reserved: SHORT;
    // Unknown
    pData : pChar;
    // Reply data buffer
    IPINFO: IPINFO;
    // Reply options
  end;

  // DLL 정의 안되있으므로 정의 하시고요..
function IcmpCreateFile: THandle; stdcall; external 'icmp.dll';
function IcmpCloseHandle(icmpHandle: THandle): boolean; stdcall; external 'icmp.dll';
function IcmpSendEcho(icmpHandle : THandle; DestinationAddress: LongInt; RequestData : Pointer; RequestSize : Smallint; RequestOptions: Pointer;
                      ReplyBuffer : Pointer; ReplySize : DWORD; Timeout : DWORD): DWORD; stdcall; external 'icmp.dll';

// 핑 나갑니다.
function Ping(IPAddress: string; msTime: Integer): Integer;
var
  Handle: THandle;
  DW: DWORD;
  REP: ICMPECHO;
  IPLong: LongInt;
begin
  Result := -1;

  Handle := IcmpCreateFile;

  if Handle = INVALID_HANDLE_VALUE then
    Exit;

  IPLong := inet_addr(pChar(IPAddress));
  DW := IcmpSendEcho(Handle, IPLong, nil, 0, nil, @REP, Sizeof(REP), msTime);

  Result := REP.Status;

  IcmpCloseHandle(Handle);
end;

// 도메인 주소를 IP로 변환 합니다.
function WWW2IP(strDomainName: string): string;
var
  pHostInfo: pHostEnt;
  wsaData: TWSAData;

  function isIP(Host: String): Integer;
  var
    sHost: string;
    is255: boolean;
  begin
    Result := -1;
    is255 := True;

    if Host = '' then
      Exit;

    sHost := Host + '.';

    While not(sHost = '') do
    begin
      case StrToIntDef(Copy(sHost, 1, Pos('.', sHost) - 1), -1) of
        0 .. 255:
          Result := 1;
        -1:
          Result := -1;
      else
        is255 := False;
      end;

      sHost := Copy(sHost, Pos('.', sHost) + 1, MaxInt);
    end;

    if not is255 then
      Result := 2;

  end;

begin

  case isIP(strDomainName) of
    1:
      begin
        Result := strDomainName;
        Exit;
      end;
    2:
      begin
        Result := '';
        Exit;
      end;
  end;

  WSAStartup(MAKEWORD(2, 2), wsaData);

  pHostInfo := GetHostByName(pChar(strDomainName));
  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;

예제

If Ping(WWW2IP('www.delmadang.com'),1000) = IP_SUCCESS Then 성공...

<<제작/편집/수정 : 김말동 >>

Author: yyjksw