在DELPHI里如何获得客户端的IP呢?有像JAVA里面request.getRemoteAddr()函数吗

解决方案 »

  1.   

    事实上就是获得装了我DELPHI程序的电脑上的IP
      

  2.   

    HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Tcpip\Parameters\Interfaces里面去取一下看看
      

  3.   

    function G_GetLocalHostName(): string;
    var
      wVersionRequested: WORD;
      wsaData: TWSAData;
      p: PHostEnt;
      s: array[0..128] of char;
    begin
      result := '';
      try
       wVersionRequested := MAKEWORD(1, 1);
       WSAStartup(wVersionRequested, wsaData);
       GetHostName(@s, 128);
       p := GetHostByName(@s);
       result := p^.h_Name;
       WSACleanup;
      except
      end;
    end;function G_GetLocalHostIp(): string;
    var
      wVersionRequested: WORD;
      wsaData: TWSAData;
      p: PHostEnt;
      s: array[0..128] of char;
    begin
    result := '';
    try
       wVersionRequested := MAKEWORD(1, 1);
       WSAStartup(wVersionRequested, wsaData);
       GetHostName(@s, 128);
       p := GetHostByName(@s);
       result := inet_ntoa(PInAddr(p^.h_addr_list^)^);
       WSACleanup();
      except
      end;
    end;客户端登录时,账号密码验证通过后,把自己的IP提交给服务端做处理就可以了。
      

  4.   

    lynmison 
    按照你上面的运行,他在wsaData: TWSAData; 这里就报
    [Error] Unit1.pas(31): Undeclared identifier: 'TWSAData'
    [Error] Unit1.pas(32): Undeclared identifier: 'PHostEnt'
    [Error] Unit1.pas(35): Undeclared identifier: 'result'
    。。
      

  5.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs,winsock, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        Edit1: TEdit;
        procedure Button1Click(Sender: TObject);  private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);type   
          TaPInAddr   =   array[0..10]   of   PInAddr;   
          PaPInAddr   =   ^TaPInAddr;   
      var   
          phe:   PHostEnt;
          pptr:   PaPInAddr;   
          Buffer:   array[0..63]   of   char;   
          I:   Integer;
          GInitData:   TWSADATA;
           Result:string;
           AName: pchar;
           asize:cardinal;  begin      asize:=MAX_COMPUTERNAME_LENGTH   +   1;
          getmem(AName,asize);
          try   
          getcomputername(AName,asize);
            finally   
          freemem(AName);
           end;     AName :='jujumao';
          WSAStartup($101,   GInitData);      StrPCopy(Buffer,   AName);      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);
              edit1.Text   :=Result;      end;   
          WSACleanup;
       end;end.
      

  6.   


    uses
    winsock//引用该单元~function G_GetLocalHostIp(): string;
    var
      wVersionRequested: WORD;
      wsaData: TWSAData;
      p: PHostEnt;
      s: array[0..128] of char;
    begin
    result :=  ' ';
    try
       wVersionRequested := MAKEWORD(1, 1);
       WSAStartup(wVersionRequested, wsaData);
       GetHostName(@s, 128);
       p := GetHostByName(@s);
       result := inet_ntoa(PInAddr(p^.h_addr_list^)^);
       WSACleanup();
      except
      end;
    end;
      

  7.   

    select SYS_CONTEXT('USERENV','IP_ADDRESS') from dual;