如何实现获取本机IP地址?

解决方案 »

  1.   

    jia ge serversocket jiu xing le
      

  2.   

    Function TFrmMain.GetLocalIP: String;
    Type
      TaPinAddr = Array[0..10] of PinAddr;
      PaPinAddr = ^TaPinAddr;
    Var
      phe : PhostEnt;
      pptr : PaPinAddr;
      Buffer : Array[0..63] of Char;
      i : Integer;
      GinitData : TWSADATA;
    begin
      WSAStartup($101,GinitData);
      Result := '';
      GetHostName(Buffer,Sizeof(Buffer));
      phe := GetHostByName(Buffer);
      if phe =nil then exit;
      pptr := PaPinAddr(phe^.h_addr_list);
      i:=0;
      while pptr^[i] <>nil do
        begin
          Result := Strpas(inet_ntoa(pptr^[i]^));
          inc(i);
        end;
      WSACleanup;
    end;
      

  3.   

    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;
      

  4.   

    function LocalIP : string;
    type
        TaPInAddr = array [0..10] of PInAddr;
        PaPInAddr = ^TaPInAddr;
    var
        phe  : PHostEnt;
        pptr : PaPInAddr;
        Buffer : array [0..63] of char;
        I    : Integer;
        GInitData      : TWSADATA;begin
        WSAStartup($101, GInitData);
        Result := '';
        GetHostName(Buffer, SizeOf(Buffer));
        phe :=GetHostByName(buffer);
        if phe = nil then Exit;
        pptr := PaPInAddr(Phe^.h_addr_list);
        I := 0;
        while pptr^[I] <> nil do begin
          result:=StrPas(inet_ntoa(pptr^[I]^));
          Inc(I);
        end;
        WSACleanup;
    end;
      

  5.   

    获取IP函数!要实现获取本机IP地址,只要把本机名赋给Name既可。
    function  TForm1.nametoip(name:string):string;
    var
      WSAData: TWSAData;
      HostEnt: PHostEnt;
    begin
      result:='';
      WSAStartup(2, WSAData);
      HostEnt := gethostbyname(PChar(name));
      if HostEnt <> nil then
      begin
        with HostEnt^ do
         result:= Format('%d.%d.%d.%d', [Byte(h_addr^[0]), Byte(h_addr^[1]), Byte(h_addr^[2]), Byte(h_addr^[3])]);
        end;
        WSACleanup;
    end;
      

  6.   

    呵呵ICS组件很简单的
    WSocket.LocalIPList.Text
    在http://www.overbyte.be    或    http://www.rtfm.be/fpiette