如何获得本机的IP地址和对应的MASK(关键是MASK如何获得),300分(300分)

  • 主题发起人 主题发起人 meckyhan
  • 开始时间 开始时间
M

meckyhan

Unregistered / Unconfirmed
GUEST, unregistred user!
如果是IP倒是好解决,一般的SOCKET都有函数提供gethostname()、gethostbyname()等
如果MASK是标准的子网掩码也好办,只要判断属于那一类IP就行,但如果不是标准的MASK
只能去查注册表了,一般在
//HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services
/Tcpip/Parameters/Interfaces/{9B641289-FF53-4E8F-8636-4065F99CBFFC}/subnetmask
下,你的机器上配置可能和我不太一样。
GOOD LUCK
 
function GetLocalIP: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);
try
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;
finally
WSACleanup;
end;
end;

讲Mask的书我放在家里了,没带过来 :(
 
教父,拜托回去查一下
 
一定 :)
 
教父:
你的获得IP函数的函数不能编译啊,array [0..10] of PInAddr,PHostEnt;PaPInAddr,
TWSADATA是什么东东啊??
 
呵呵,忘了说一句:要uses Winsock单元
还有,那本书我找不到了,真是不好意思 :(
 
我知道了,用IpHlpApi中的函数
 
我想你还可以实施这个方法:
procedure Tform1.Button1Click(Sender:TObject);
begin
showmessage('ip address of your computer is'+nmftp1.LocalIP);
end;
用到了TNMFTP控件,但也不难
 
function nametoip(name:string):string; //通过机器名得到IP地址
var
WSAData: TWSAData;
HostEnt: PHostEnt;
begin
result:='';
WSAStartup(2, WSAData);
HostEnt := gethostbyname(PChar(name));
if HostEnt <> nil then
begin
with HostEnt^ do
result:= Format('%d.%d.%d.%d',
[Byte(h_addr^[0]), Byte(h_addr^[1]),
Byte(h_addr^[2]), Byte(h_addr^[3])]);
end;
WSACleanup;
end;
 
>获取IP:

function GetIPFromName(Name: string): string;
var
WSAData: TWSAData;
HostEnt: PHostEnt;
begin
WSAStartup(2, WSAData);
HostEnt := gethostbyname(PChar(Name));
with HostEnt^ do
Result := Format('%d.%d.%d.%d', [Byte(h_addr^[0]),
Byte(h_addr^[1]), Byte(h_addr^[2]), Byte(h_addr^[3])]);
WSACleanup;
end;

>获取MASK:

当前机器的设置可以从注册表
HKEY_LOCAL_MACHINE/System/CurrentControlSet/
Services/Class/NetTrans/中得到。
搜索从0001到000a,如果
DriverDesc =TCP/IP,则IPMask就是当前掩码
 
如果在NT中则先找服务器网卡(也许有双网卡)名字
HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows NT/CurrentVersion/NetworkCards/1/ServiceName
找到后所对应网卡的IP地址和 MASK就在这点-----》
HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/"ServiceName"/Parameters/Tcpip
IpAddress是IP地址,SubnetMask是子网掩码,DefaultGateway就是网关了。。。。。。

 
注册表

//HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services
/Tcpip/Parameters/Interfaces/{9B641289-FF53-4E8F-8636-4065F99CBFFC}/subnetmask

 
可能只有找注册表了。
 
咦? 上次那个谁不是回答过了吗? 我还在Delphi中试了一下,可以实现你的要求了的!
奇怪,怎么又问一次。 哪个贴子我也忘了,不过代码如下:
unit uSock;

interface

uses Windows, Winsock;

{ 此函数列举出所有的TCP/IP连接,并返回一个由回车换行(CRLF)符分隔的字符串,
包含以下信息: IP, NetMask, BroadCast-Address, Up/Down status, Broadcast support, Loopback
如果你将这个字符串赋给TMemo(它的Memo.Lines.Text属性),你可以看到更清晰的结果。
使用此函数,你需要Win98/ME/2K, 95 OSR 2 或者NT service pack #3,
因为程序会使用到WinSock 2(WS2_32.DLL)。 }
{ 从Winsock 2.0导入函数WSAIOCtl -- 在Win98/ME/2K and 95 OSR2, NT srv pack #3下才有Winsock 2 }

function EnumInterfaces(var sInt: string): Boolean;
function WSAIoctl(s: TSocket; cmd: DWORD; lpInBuffer: PCHAR;
dwInBufferLen: DWORD; lpOutBuffer: PCHAR; dwOutBufferLen: DWORD;
lpdwOutBytesReturned: LPDWORD; lpOverLapped: POINTER;
lpOverLappedRoutine: POINTER): Integer; stdcall; external 'WS2_32.DLL';

{ Constants taken from C header files }
const
SIO_GET_INTERFACE_LIST = $4004747F;
IFF_UP = $00000001;
IFF_BROADCAST = $00000002;
IFF_LOOPBACK = $00000004;
IFF_POINTTOPOINT = $00000008;
IFF_MULTICAST = $00000010;
type
sockaddr_gen = packed record
AddressIn: sockaddr_in;
filler: packed array[0..7] of char;
end;
type
INTERFACE_INFO = packed record iiFlags: u_long; // Interface flags
iiAddress: sockaddr_gen; // Interface address
iiBroadcastAddress: sockaddr_gen; // Broadcast address
iiNetmask: sockaddr_gen; // Network mask
end;

implementation
{-------------------------------------------------------------------
1. 打开Winsock
2. 创建一个socket
3. 调用WSAIOCtl获取网络连接
4. 对每个连接,获取它的IP、掩码、广播地址、状态
5. 将信息填充到一个由CDLF分隔的字符串中
6. 结束
--------------------------------------------------------------------}

function EnumInterfaces(var sInt: string): Boolean;
var
s: TSocket;
wsaD: WSADATA;
NumInterfaces: Integer;
BytesReturned, SetFlags: u_long;
pAddrInet: SOCKADDR_IN;
pAddrString: PCHAR;
PtrA: pointer;
Buffer: array[0..20] of INTERFACE_INFO;
i: Integer;
begin
result := true; // Initialize sInt := '';
WSAStartup($0101, wsaD);
// Start WinSock // You should normally check // for errors here :)
s := Socket(AF_INET, SOCK_STREAM, 0); // Open a socket
if (s = INVALID_SOCKET) then
exit;
try // Call WSAIoCtl
PtrA := @bytesReturned;
if (WSAIoCtl(s, SIO_GET_INTERFACE_LIST, nil, 0, @Buffer, 1024, PtrA, nil,
nil) <> SOCKET_ERROR) then
begin // If ok, find out how // many interfaces exist
NumInterfaces := BytesReturned div SizeOf(INTERFACE_INFO);
for i := 0 to NumInterfaces - 1 do // For every interface
begin
pAddrInet := Buffer.iiAddress.addressIn; // IP ADDRESS
pAddrString := inet_ntoa(pAddrInet.sin_addr);
sInt := sInt + ' IP=' + pAddrString + ',';
pAddrInet := Buffer.iiNetMask.addressIn; // SUBNET MASK
pAddrString := inet_ntoa(pAddrInet.sin_addr);
sInt := sInt + ' Mask=' + pAddrString + ',';
pAddrInet := Buffer.iiBroadCastAddress.addressIn; // Broadcast addr
pAddrString := inet_ntoa(pAddrInet.sin_addr);
sInt := sInt + ' Broadcast=' + pAddrString + ',';
SetFlags := Buffer.iiFlags;
if (SetFlags and IFF_UP) = IFF_UP then
sInt := sInt + ' Interface UP,' // Interface up/down
else
sInt := sInt + ' Interface DOWN,';
if (SetFlags and IFF_BROADCAST) = IFF_BROADCAST then // Broadcasts
sInt := sInt + ' Broadcasts supported,' // supported or
else // not supported
sInt := sInt + ' Broadcasts NOT supported,';
if (SetFlags and IFF_LOOPBACK) = IFF_LOOPBACK then // Loopback or
sInt := sInt + ' Loopback interface'
else
sInt := sInt + ' Network interface'; // normal
sInt := sInt + #13#10; // CRLF between // each interface
end;
end;
except
end; // // Close sockets
// CloseSocket(s);
WSACleanUp;
result := false;
end;

end.


调用很简单:
procedure TForm1.Button1Click(Sender: TObject);
var
S: string;
begin
EnumInterfaces(S);
memo1.Text := S;
end;
在我机上结果如下:
IP=192.0.0.1, Mask=255.255.255.0, Broadcast=255.255.255.255, Interface UP, Broadcasts supported, Network interface
IP=127.0.0.1, Mask=255.0.0.0, Broadcast=255.255.255.255, Interface UP, Broadcasts supported, Loopback interface
已经实现你的目的的啊?
 
用sock调用,win2k下拔了网线就取不到ip和mask了,用iphelper就没问题,
代码在:
http://www.8421.org/download.php?id=147
 
后退
顶部