O
oldwolf202
Unregistered / Unconfirmed
GUEST, unregistred user!
我在写一个网关程序,现在我要在网关上动态分配IP,而主机名是不变的,我想根据主机名来取IP。我写的程序如下:
char FAR mname[255];
int iErrorCode;
struct WSAData *WSAData;
WSAStartup(2,WSAData);
iErrorCode = gethostname(mname, 255);
struct hostent FAR * pHostent;
pHostent = (struct hostent * )malloc(sizeof(struct hostent));
pHostent = gethostbyname(mname);
ShowMessage(mname);
程序单步调试时MNAME的值一直为一串0 !
是出了什么问题呢?
后来有个朋友帮我写了一个DELPHI下的程序:
使用 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;
请问上面的程序怎么改写成c++builder程序?
我改了半天总是有问题,请高手帮助?
或者有没有其他更好的方法?
char FAR mname[255];
int iErrorCode;
struct WSAData *WSAData;
WSAStartup(2,WSAData);
iErrorCode = gethostname(mname, 255);
struct hostent FAR * pHostent;
pHostent = (struct hostent * )malloc(sizeof(struct hostent));
pHostent = gethostbyname(mname);
ShowMessage(mname);
程序单步调试时MNAME的值一直为一串0 !
是出了什么问题呢?
后来有个朋友帮我写了一个DELPHI下的程序:
使用 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;
请问上面的程序怎么改写成c++builder程序?
我改了半天总是有问题,请高手帮助?
或者有没有其他更好的方法?