请教如何在程序里获得本机或客户机的IP地址啊,请教代码是怎样的(0分)

  • 主题发起人 主题发起人 topdelphi
  • 开始时间 开始时间
T

topdelphi

Unregistered / Unconfirmed
GUEST, unregistred user!
请教如何在程序里获得本机或客户机的IP地址啊,请教代码是怎样的
 
我有通过计算机名取得IP地址,通过IP地址取得计算机名的源程序,需要的话留Email
 
另外,我看了你的个人情况:
topdelphi 有: 66个待答问题, 8个已答问题, 回答了: 1 个问题。
别老是问问题,多回答些,赚点积分。既帮别人也帮自己呀。
 
谢谢各位,我的E-MAIL是
say-forevers@163.com
 
Program MyIP;
{$APPTYPE CONSOLE}
Uses
WinSock;
Var
wsaData : TWSAData;
Begin
WSAStartup( 257, wsaData );
WriteLn( 'Host: ', GetHostByName( NIL )^.h_name );
Write ( 'IP : ', iNet_ntoa( PInAddr( GetHostByName( NIL )^.h_addr_list^ )^ ) );
WSACleanup;
End.

 
给你个函数:(保证好用,呵。。。)
function 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^ <> nil do
begin
result := StrPas(inet_ntoa(pptr^^));
Inc(I);
end;
WSACleanup;
end;
 
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs
,winsock, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation

{$R *.DFM}

procedure ShowHostName;
var
wVersionRequested : WORD;
wsaData : TWSAData;
p : PHostEnt;
s : array[0..128] of char;
p2 : pchar;
begin
{启动 WinSock}
wVersionRequested := MAKEWORD(1, 1);
WSAStartup(wVersionRequested, wsaData);
{计算机名}
GetHostName(@s,128);
p := GetHostByName(@s);
ShowMessage(Format('Computer name is ''%s''.', [p^.h_Name]));
{IP地址}
p2 := iNet_ntoa(PInAddr(p^.h_addr_list^)^);
ShowMessage(Format('IP address is %s.',[p2]));
WSACleanup;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowHostName;
end;
end.
 
接受答案了.
 

Similar threads

后退
顶部