如何实现域名解析功能。(100分)

  • 主题发起人 主题发起人 niuqi31
  • 开始时间 开始时间
N

niuqi31

Unregistered / Unconfirmed
GUEST, unregistred user!
原来和远程PC通讯都是用IP端口,现在想通过域名和其通讯。即pop3.sina.com.cn 解析成202。168。192。65
 
免费的方案——
注册一个花生壳的二级域名,再下载一个花生壳客户端
 
function GetIP(Name:string) : string;
type
TaPInAddr = array [0..10] of PInAddr;
PaPInAddr = ^TaPInAddr;
var
phe :PHostEnt;
pptr : PaPInAddr;
I : Integer;
GInitData : TWSADATA;
begin
WSAStartup($101, GInitData);
Result := '';
phe :=GetHostByName(pchar(Name)); //返回一个指向主机信息结构的指针
pptr := PaPInAddr(Phe^.h_addr_list); //强制转换
result:=StrPas(inet_ntoa(pptr^[0]^));//返回一个点分形式的字符串IP地址
WSACleanup;
end;

 
多谢二位。
to bundur:花生壳早就听说了,好像是做个人动态IP解析的,不知现在是否收费了?
to jianguobu 你的函数我回头试一下。请问是同步方式的吗?
 
to jianguobu:TaPInAddr,PaPInAddr,TWSADATA找不到声明,谢谢,盼复。
 
interface 接口部分加
uses winsock;

注意 GetHostByName 可能会有阻塞,如果编写服务程序应该用异步方式WSAAsyncGetHostByName.
 
干脆我把它发给你吧.
 
效果棒极了,谢谢各位大侠。请问哪位有for c++b的这段函数。[:)]
 
有好东东 就要共享一下吗,~~~~~
 
unit UnitDNS;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Winsock;
const
WM_RESOLVED = WM_USER+100;
Type
TWMRESOLVED = record
Msg: WORD;
wParam:Word;
lParam:DWORD;
end;
type
TFormMain = class(TForm)
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
EditPresentation: TEdit;
edtHost: TEdit;
BtnWSAAsyncGetHostByName: TButton;
BtnWSACancelAsyncRequest: TButton;
EditResult: TEdit;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure BtnWSAAsyncGetHostByNameClick(Sender: TObject);
procedure BtnWSACancelAsyncRequestClick(Sender: TObject);
private
{ Private declarations }
procedure WMRESOLVED(var Msg:TWMRESOLVED);Message WM_RESOLVED;
public
{ Public declarations }

end;

var
FormMain: TFormMain;
buf:HOSTENT;
hAsyncTaskHandle:THandle;
implementation

{$R *.DFM}
procedure TFormMain.WMRESOLVED(var Msg:TWMRESOLVED);
begin
if buf.h_addr=NIL then
Begin
EditResult.Text:='Failed to resolve';
exit;
end;
EditResult.Text:=Format('%d.%d.%d.%d',
[ord( buf.h_addr^[0]),
ord(buf.h_addr^[1]),
ord(buf.h_addr^[2]),
ord(buf.h_addr^[3])]);

edtHost.Text:=buf.h_name;
end;
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.BtnWSAAsyncGetHostByNameClick(Sender: TObject);

begin
hAsyncTaskHandle:=WSAAsyncGetHostByName(Handle,WM_RESOLVED,pchar(EditPresentation.Text),pchar(@buf),MAXGETHOSTSTRUCT);
end;

procedure TFormMain.BtnWSACancelAsyncRequestClick(Sender: TObject);
begin
WSACancelAsyncRequest(hAsyncTaskHandle);
end;

end.

 
请问哪位有for c++b的这段函数.
 
谢谢各位。
 
后退
顶部