gethostbyname 的bug(100分)

  • 主题发起人 主题发起人 wg168
  • 开始时间 开始时间
W

wg168

Unregistered / Unconfirmed
GUEST, unregistred user!
我用 gethostbyname 在有些机器上为什么 返回的 ip 为 192.168.0.0。实际<br>机器ip是 192.168.0.2?
 
function LocalIP: string;<br> &nbsp;type<br> &nbsp; &nbsp;TaPInAddr = array[0..10] of PInAddr;<br> &nbsp; &nbsp;PaPInAddr = ^TaPInAddr;<br> &nbsp;var<br> &nbsp; &nbsp;phe: PHostEnt;<br> &nbsp; &nbsp;pptr: PaPInAddr;<br> &nbsp; &nbsp;Buffer: array[0..63] of Char;<br> &nbsp; &nbsp;I: Integer;<br> &nbsp; &nbsp;GInitData: TWSAData;<br> &nbsp;begin<br> &nbsp; &nbsp;WSAStartup($101, GInitData);<br> &nbsp; &nbsp;Result := '';<br> &nbsp; &nbsp;GetHostName(Buffer, SizeOf(Buffer));<br> &nbsp; &nbsp;phe := GetHostByName(buffer);<br> &nbsp; &nbsp;if phe = nil then Exit;<br> &nbsp; &nbsp;pPtr := PaPInAddr(phe^.h_addr_list);<br> &nbsp; &nbsp;I := 0;<br> &nbsp; &nbsp;while pPtr^ &lt;&gt; nil do<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;Result := inet_ntoa(pptr^^);<br> &nbsp; &nbsp; &nbsp;Inc(I);<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;WSACleanup;<br> &nbsp;end;
 
我用这种为什么有问题? &nbsp; <br><br> &nbsp; &nbsp; &nbsp; ConsultResult := Copy(Host.h_addr^, 0, 4);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Result := Format('%d.%d.%d.%d',<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[ord(ConsultResult[1]), ord(ConsultResult[2]),<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ord(ConsultResult[3]), ord(ConsultResult[4])]);<br><br>用楼上的就没有问题 <br>pPtr := PaPInAddr(phe^.h_addr_list);<br>Result := inet_ntoa(pptr^^);
 
后退
顶部