www.Alltips.Com 2001-12-15 极限技术网
中文开发在线原创作品,未经允许,严禁任何形式的转载
在use子句声明Winsock
............
function HostToIP(Name: string; var Ip: string): Boolean;
var
wsdata : TWSAData;
hostName : array [0..255] of char;
hostEnt : PHostEnt;
addr : PChar;
begin
WSAStartup ($0101, wsdata);
try
gethostname (hostName, sizeof (hostName));
StrPCopy(hostName, Name);
hostEnt := gethostbyname (hostName);
if Assigned (hostEnt) then
if Assigned (hostEnt^.h_addr_list) then begin
addr := hostEnt^.h_addr_list^;
if Assigned (addr) then begin
IP := Format ('%d.%d.%d.%d', [byte (addr [0]),
byte (addr [1]), byte (addr [2]), byte (addr [3])]);
Result := True;
end
else
Result := False;
end
else
Result := False
else begin
Result := False;
end;
finally
WSACleanup;
end
end;
................................
你可以这样试用这个功能:在form上放置EditBox、一个按钮和一个标签,在把代码加到按钮的OnClick事件上
procedure TForm1.Button1Click(Sender: TObject);
var
IP: string;
begin
if HostToIp(Edit1.Text, IP) then Label1.Caption := IP;
end;
有高手的朋友请帮我看看以上的代码,可以给我点注释吗?我想知道每一行的代码的含义!
谢谢您的帮助:)改天请大家吃冰淇淋~~~
4 个解决方案
#1
function HostToIP(Name: string; var Ip: string): Boolean;
var
wsdata : TWSAData;
hostName : array [0..255] of char;
hostEnt : PHostEnt;
addr : PChar;
begin
WSAStartup ($0101, wsdata); //获取Winsock的版本为1.1的动态链接库
try
gethostname (hostName, sizeof (hostName)); //获取本机的主机名称
StrPCopy(hostName, Name); //转换为PChar字符串
hostEnt := gethostbyname (hostName); //通过主机名称获得IP地址
if Assigned (hostEnt) then
if Assigned (hostEnt^.h_addr_list) then begin
addr := hostEnt^.h_addr_list^;
if Assigned (addr) then begin //格式化输出
IP := Format ('%d.%d.%d.%d', [byte (addr [0]),
byte (addr [1]), byte (addr [2]), byte (addr [3])]);
Result := True;
end
else
Result := False;
end
else
Result := False
else begin
Result := False;
end;
finally
WSACleanup;
end
end;
var
wsdata : TWSAData;
hostName : array [0..255] of char;
hostEnt : PHostEnt;
addr : PChar;
begin
WSAStartup ($0101, wsdata); //获取Winsock的版本为1.1的动态链接库
try
gethostname (hostName, sizeof (hostName)); //获取本机的主机名称
StrPCopy(hostName, Name); //转换为PChar字符串
hostEnt := gethostbyname (hostName); //通过主机名称获得IP地址
if Assigned (hostEnt) then
if Assigned (hostEnt^.h_addr_list) then begin
addr := hostEnt^.h_addr_list^;
if Assigned (addr) then begin //格式化输出
IP := Format ('%d.%d.%d.%d', [byte (addr [0]),
byte (addr [1]), byte (addr [2]), byte (addr [3])]);
Result := True;
end
else
Result := False;
end
else
Result := False
else begin
Result := False;
end;
finally
WSACleanup;
end
end;
#2
WSACleanUp 是释放所有的内存.
#3
tre
#4
学习
#1
function HostToIP(Name: string; var Ip: string): Boolean;
var
wsdata : TWSAData;
hostName : array [0..255] of char;
hostEnt : PHostEnt;
addr : PChar;
begin
WSAStartup ($0101, wsdata); //获取Winsock的版本为1.1的动态链接库
try
gethostname (hostName, sizeof (hostName)); //获取本机的主机名称
StrPCopy(hostName, Name); //转换为PChar字符串
hostEnt := gethostbyname (hostName); //通过主机名称获得IP地址
if Assigned (hostEnt) then
if Assigned (hostEnt^.h_addr_list) then begin
addr := hostEnt^.h_addr_list^;
if Assigned (addr) then begin //格式化输出
IP := Format ('%d.%d.%d.%d', [byte (addr [0]),
byte (addr [1]), byte (addr [2]), byte (addr [3])]);
Result := True;
end
else
Result := False;
end
else
Result := False
else begin
Result := False;
end;
finally
WSACleanup;
end
end;
var
wsdata : TWSAData;
hostName : array [0..255] of char;
hostEnt : PHostEnt;
addr : PChar;
begin
WSAStartup ($0101, wsdata); //获取Winsock的版本为1.1的动态链接库
try
gethostname (hostName, sizeof (hostName)); //获取本机的主机名称
StrPCopy(hostName, Name); //转换为PChar字符串
hostEnt := gethostbyname (hostName); //通过主机名称获得IP地址
if Assigned (hostEnt) then
if Assigned (hostEnt^.h_addr_list) then begin
addr := hostEnt^.h_addr_list^;
if Assigned (addr) then begin //格式化输出
IP := Format ('%d.%d.%d.%d', [byte (addr [0]),
byte (addr [1]), byte (addr [2]), byte (addr [3])]);
Result := True;
end
else
Result := False;
end
else
Result := False
else begin
Result := False;
end;
finally
WSACleanup;
end
end;
#2
WSACleanUp 是释放所有的内存.
#3
tre
#4
学习