请问如何根据机器名得到机器的ip地址?

解决方案 »

  1.   

    这个问题我做过。用到windows API的函数调用,可查一下很多网站有源代码。
      

  2.   

    用API;GetAddressByNameINT GetAddressByName(    DWORD dwNameSpace, // name space to query for service address information 
        LPGUID lpServiceType, // the type of the service
        LPTSTR lpServiceName, // the name of the service
        LPINT lpiProtocols, // points to array of protocol identifiers
        DWORD dwResolution, // set of bit flags that specify aspects of name resolution
        LPSERVICE_ASYNC_INFO lpServiceAsyncInfo, // reserved for future use, must be NULL
        LPVOID lpCsaddrBuffer, // points to buffer to receive address information
        LPDWORD lpdwBufferLength, // points to variable with address buffer size information
        LPTSTR lpAliasBuffer, // points to buffer to receive alias information
        LPDWORD lpdwAliasBufferLength  // points to variable with alias buffer size information
       );
      

  3.   

    procedure TForm2.FormCreate(Sender: TObject);
    var
    WSAData:TWSAData;
    begin
    if (wsastartup(MAKEWORD(2,0),WSADATA)<>0 ) THEN
            begin
            raise exception.Create('Socket version error!');
            end;
    end;procedure TForm2.FormDestroy(Sender: TObject);
    begin
    wsacleanup;
    end;procedure TForm2.Button1Click(Sender: TObject);
    var
    host:phostent;
    result:string;
    begin
    try
            host:=GETHOSTBYNAME('newry');
    EXCEPT
            SHOWMESSAGE('ERROR!');
            EXIT;
    END;
    edit1.text:=host.h_name;
    RESULT:=COPY(HOST.H_ADDR^,0,4);edit1.text:=format('%d.%d.%d.%d',
                                    [ord(result[1]),
                                     ord(result[2]),
                                     ord(result[3]),
                                     ord(result[5])]);end;
      

  4.   

    uses WinSock;
    ...
    type
      TAddrList = array[0..5] of PInAddr;
      PAddrList = ^TAddrList;
    var
      AName: array[0..255] of Char;
      ASize: Cardinal;
      AHost: PHostEnt;
      Addrs: PAddrList;
      WSA: WSAData;
      I: Integer;
      IP: String;
    begin
      ASize := 0;
      FillChar(AName, 255, 0);
      WSAStartup(MakeWord(1, 1), WSA);
      try
        gethostname(AName, ASize); // 取当前机器名
        AHost := gethostbyname(AName);
        Addrs := Pointer(AHost.h_addr_list); // h_addr_list指向ip表
        I := 0;
        while Addrs[I] <> nil do // 可能不止一个ip
        begin
          IP := String(inet_ntoa(Addrs^[I]^)); // 转换成字符串形式的ip
          //Memo1.Add(IP);
          Inc(I);
        end;
      finally;
        WSACleanup;
      end;
    end;
      

  5.   

    procedure TForm1.Button4Click(Sender: TObject);
    var Ip,IpStr:string;  ch:array[1..32]of char;  i:integer;
        WSData:TWSAData;  MyHost:PHostEnt;
    begin
       if WSAstartup(2,WSData)<>0 then
          begin
             ShowMessage('没有成功返回!');
             Halt(2);
          end;
       try
          if getHostName(@ch[1],32)<>0 then
             begin
                ShowMessage('没有成功返回!');
                Halt(3);
             end;
       except
          ShowMessage('没有成功返回!');
          Halt(3);
       end;
       MyHost:=GetHostByName(@ch[1]);
       if MyHost=NIL then
          begin
             ShowMessage('没有成功返回!');
             Halt(4);
          end
       else
          begin
             for i:=1 to 4 do
                begin
                   Ip:=inttostr(Ord(MyHost.h_addr^[i-1]));
                   ShowMessage('IP分段地址为:'+Ip);
                   IPStr:=IPStr+Ip;
                   if i<4 then IPStr:=IPStr+'.'
                   else  ShowMessage('IP地址为:'+IPStr);
                end;
          end;
    end;