已经知道网卡的物理地址,可否得到网卡的ip,或者电脑的名称?(50分)

  • 主题发起人 主题发起人 HeBaisong
  • 开始时间 开始时间
H

HeBaisong

Unregistered / Unconfirmed
GUEST, unregistred user!
问题如题
 
program get_ip;
uses
winsock,sysutils;
VAR
ch : ARRAY[1..32] OF Char;
i : Integer;
WSData: TWSAData;
MyHost: PHostEnt;
begin
IF WSAstartup(2,wsdata)<>0 THEN
BEGIN
Writeln('can''t start Winsock: Error ',WSAGetLastError);
Halt(2);
END;
try
IF getHostName(@ch[1],32)<>0 THEN
BEGIN
Writeln('getHostName failed');
Halt(3);
END;
except
Writeln('getHostName failed');
halt(3);
end;
MyHost:=GetHostByName(@ch[1]);
IF MyHost=NIL THEN
BEGIN
Writeln(GetHostName('+StrPas(@ch[1])+') failed : Error
'+IntToStr(WSAGetLastError));
Halt(4);
END
ELSE
BEGIN
Write('address ');
FOR i:=1 TO 4 DO
BEGIN
Write(Ord(MyHost.h_addr^[i-1]));
IF i<4 THEN
write('.')
ELSE
writeln;
END;
END;
end.
 
解析IP地址为主机域名。

使用 WinSock 单元;
过程如下:
function IPAddrToName(IPAddr : String): String;
var
SockAddrIn: TSockAddrIn;
HostEnt: PHostEnt;
WSAData: TWSAData;
begin
WSAStartup($101, WSAData);
SockAddrIn.sin_addr.s_addr:= inet_addr(PChar(IPAddr));
HostEnt:= gethostbyaddr(@SockAddrIn.sin_addr.S_addr, 4, AF_INET);
if HostEnt<>nil then
begin
result:=StrPas(Hostent^.h_name)
end
else
begin
result:='';
end;
end;


测试时请在在线状态。

测试代码:
var
Name: string;
begin
Name := InputBox('输入主机IP', '主机IP地址:', '');
showmessage(IpAddrToName(Name));

end;

******************************
//uses winsock; 通过IP地址得到机器名
function iptoname(ip:string):string;
var
WSAData:TWSAData;
p:PHostEnt;
InetAddr:dword;
begin
result:='''';
WSAStartup(2, WSAData);
InetAddr:= inet_addr(PChar(IP));
try
p:=GetHostByAddr(@InetAddr, Length(IP), PF_Inet);
if p<> nil then result:=p^.h_name;
except
result:='''';
end;
end;
这段代码我试过, 不过只对局域网的机器有效,对internet上的机器不行,且速度不太快

我有看到相关的资料,是通过udp协议,往远程机器的137端口发送UDP报文,远程机器会返回
数据,其中就有远程机器的当前用户名,机器名称等信息,但如何具体实现我就不太清楚了
 
获得本机IP:
function LocalIP: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;
========================================
program get_ip;
uses
winsock,sysutils;
VAR
ch : ARRAY[1..32] OF Char;
i : Integer;
WSData: TWSAData;
MyHost: PHostEnt;
begin
IF WSAstartup(2,wsdata)<>0 THEN
BEGIN
Writeln('can''t start Winsock: Error ',WSAGetLastError);
Halt(2);
END;
try
IF getHostName(@ch[1],32)<>0 THEN
BEGIN
Writeln('getHostName failed');
Halt(3);
END;
except
Writeln('getHostName failed');
halt(3);
end;
MyHost:=GetHostByName(@ch[1]);
IF MyHost=NIL THEN
BEGIN
Writeln(GetHostName('+StrPas(@ch[1])+') failed : Error
'+IntToStr(WSAGetLastError));
Halt(4);
END
ELSE
BEGIN
Write('address ');
FOR i:=1 TO 4 DO
BEGIN
Write(Ord(MyHost.h_addr^[i-1]));
IF i<4 THEN
then write('.')
ELSE
writeln;
END;
END;
end.
===========================================
 
取IP啊?我也来一个:
function GetLocalIP(var LocalIp: string): Boolean;
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;
 
后退
顶部