请问如何用delphi 7 实现显示自己的ip地址 最好能提供代码 本人是初学者
请多指教!
请多指教!
解决方案 »
- 请教一下idTcpServer与idTcpClient通讯的问题
- 关于数据库连接的问题
- ADOQuery修改帶有觸發器的表為何在保存時出錯?
- 用ADO拷贝远程数据的时候,如何强制结束线程?
- 怎么让DBGrid的某列的数据显示为红色
- 各位前辈,能给介绍几本学习delphi的好书吗??
- 请问,用DELPHI怎样判断当前系统安装在哪个盘上??
- 应用程序服务器怎样根据IP限制客户端的连接???急!!!!!!!!!
- 请问XPMenu如何使toolbar上的mainmenu也具有XP风格呢?
- api 函数GetCommModemStatus 怎么用,
- 谢谢版主!从新做人!
- 高手请帮我检查一下程序那里出问题,很希望得到你们的帮助
//uses中加winsock
//介绍wsadata,phostent msdn
//另外gethostaddress,
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,winsock;type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
StaticText1: TStaticText;
StaticText2: TStaticText;
StaticText3: TStaticText;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);
var
Ip:string;
Ipstr:string;
buffer:array[1..32] of char;
i:integer;
WSData:TWSAData;
Host:PHostEnt;
begin
if WSAstartup(2,WSData)<>0 then //为程序使用WS2_32.DLL初始化
begin
showmessage('WS2_32.DLL初始化失败!');
halt;
end;
try
if gethostname(@buffer[1],32)<>0 then
begin
showmessage('没有得到主机名!');
halt;
end;except
showmessage('没有成功返回主机名');
halt;
end;
Host:=gethostbyname(@buffer[1]);
if Host=nil then
begin
showmessage('IP地址为空!');
halt;
end
else
begin
edit2.text:=host.h_name;
edit3.text:=chr(host.h_addrtype+64);
for i:=1 to 4 do
begin
Ip:=inttostr(Ord(Host.h_addr^[i-1]));
showmessage('分段Ip地址为:'+Ip);
Ipstr:=Ipstr+Ip;
if i<4 then
Ipstr:=Ipstr+'.'
else
edit1.text:=Ipstr;
end;
end;
end;end.
//WSAstartup在使用gethostname,gethostbyname前
//一定不要忘了初始化WS2_32.DLL
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,winsock;type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);
var
WSAData: TWSAData;
HostEnt: PHostEnt;
sComputerName, sIP: string;
begin
sComputername:=edit1.text;
WSAStartup(2, WSAData);
HostEnt := gethostbyname(PChar(sComputerName));
if HostEnt <> nil then
begin
with HostEnt^ do
sIP := Format('%d.%d.%d.%d', [Byte(h_addr^[0]), Byte(h_addr^[1]), Byte(h_addr^[2]), Byte(h_addr^[3])]); end;
WSACleanup;
edit2.text:=sIP;
end;
end.
///////////////////////////////////
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;
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, winsock, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
function GetHostIP(HostName: String): String;
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}function Tform1.GetHostIP(HostName: String): String;
var
buf:pChar;
iWsaRet:Integer;
Data:WSAData;
hostent:PHostEnt;
begin
Result := '';
iWsaRet := WSAStartup($101,Data);
if iWsaRet<>0 then
begin
ShowMessage('Socket initialize error!');
Exit;
end;
buf := Allocmem(60);
strcopy(buf,PChar(HostName));
if Trim(buf)='' then
gethostname(buf,60);
hostent := gethostbyname(buf);
Freemem(buf,60);
if hostent=nil then
Exit;
Result := inet_ntoa(pinAddr(hostent^.h_addr^)^);
WSACleanup();
end;
end.
如果hostname为空,则取本机的IP
var
HostEnt: PHostEnt;
Ip: string;
addr: pchar;
Buffer: array [0..63] of char;
GInitData: TWSADATA;
begin
Result := False;
try
WSAStartup(2, GInitData);
GetHostName(Buffer, SizeOf(Buffer));
HostEnt := GetHostByName(buffer);
if HostEnt = nil then Exit;
addr := HostEnt^.h_addr_list^;
ip := Format('%d.%d.%d.%d', [byte(addr [0]),
byte (addr [1]), byte (addr [2]), byte (addr [3])]);
LocalIp := Ip;
Result := True;
finally
WSACleanup;
end;
end;