在局域网内实现IP与主机名的转换,
给定主机名测试出其IP
给定主机名测试出其IP
解决方案 »
- 操作DBGrid时,如何让 ClientDataSet.RecNo 不动??
- 如何实现 多行 列内容 相同的 行合并?????????????????
- 请教各位!如何保存TImageList中的BMP文件呀,它没有SavetoFile该类的函数呀!
- Delphi开发的三层系统客户端不能登陆问题
- 我想知道哪里有DELPHI6英文版的最新升级包,给个链接,不盛感激!!!!
- 如何根据已有的日期计算某人的年龄??
- 关于beforepost响应和post自相矛盾的问题!
- 关于主程序与动态链接库调用的问题.
- 有哪位知道在如何用程序控制excel中的打印机设置?
- 何谓 Delphi ??
- [email protected]我是西安的一名程序员(Delphi数据库开发)愿意交流者将我加入联系人
- 如何给DBGRID增加这样的列?
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,Winsock;type
TFormMain = class(TForm)
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
EditPresentation: TEdit;
EditResult: TEdit;
edtHost: TEdit;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
FormMain: TFormMain;implementation{$R *.DFM}procedure TFormMain.FormCreate(Sender: TObject);
Var
WSAData:TWSAData;
begin
//initiates use of WS2_32.DLL
if (WSAStartup(MAKEWORD(2,0),WSAData)<>0) then
raise Exception.Create('Winsock Version Error');
end;procedure TFormMain.FormDestroy(Sender: TObject);
begin
WSACleanUP;
end;
procedure TFormMain.Button1Click(Sender: TObject);
var
Host: pHostent;
ConsultResult:String;
Err:Integer;
begin
Try
Host := GetHostByName(PChar(EditPresentation.Text));
Except
Err:=WSAGetLastError();
EditResult.Text:='Error Code:'+InttoStr(Err);
Exit;
end;
if Host=NIL then
begin
EditResult.Text:='Host does not exist';
exit;
end;
ConsultResult:=Copy(Host.h_addr^,0,4);
EditResult.Text:=Format('%d.%d.%d.%d',
[ord(ConsultResult[1]),
ord(ConsultResult[2]),
ord(ConsultResult[3]),
ord(ConsultResult[4])]);
edtHost.Text:=Host.h_name;
end;end.
USES
WinSock;
.....
private
hAsyncTaskHandle: THandle;
procedure WMRESOLVED(var msg:TMSG);
.....
var
buff: HOSTENT;
.....
begin
hAsyncTaskHandle := WSAAsyncGetHostByName(Handle, WM_RESOLVED,
PChar(<域名>),PChar(@buff),
MAXGETHOSTSTRUCT);
end;
procedure TForm1.WMRESOLVED(var msg: TMSG)
begin
if buf.h_addr = nil then
<出错>
else
begin
<主机名> := buff.h_name;
<IP> := Format('%d.%d.%d.%d',
[ord(ConsultResult[1]), ord(ConsultResult[2]),
ord(ConsultResult[3]),ord(ConsultResult[4])]);
end;
end;
.....
如果等待时间过长可用 WSACancelAsyncRequest(hAsyncTaskHandle); 中断解析。
USES
WinSock;
const
WM_RESOLVED = WM_USER + 1001;
.....
private
hAsyncTaskHandle: THandle;
procedure WMRESOLVED(var msg:TMSG); message WM_RESOLVED;
.....
var
buff: HOSTENT;
.....
begin
hAsyncTaskHandle := WSAAsyncGetHostByName(Handle, WM_RESOLVED,
PChar(<域名>),PChar(@buff),
MAXGETHOSTSTRUCT);
end;
procedure TForm1.WMRESOLVED(var msg: TMSG)
begin
if buf.h_addr = nil then
<出错>
else
begin
<主机名> := buff.h_name;
<IP> := Format('%d.%d.%d.%d',
[ord(ConsultResult[1]), ord(ConsultResult[2]),
ord(ConsultResult[3]),ord(ConsultResult[4])]);
end;
end;
.....
如果等待时间过长可用 WSACancelAsyncRequest(hAsyncTaskHandle); 中断解析。
刚才写的有点错,不好意思。