栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

奇数InetAddress.isReachable()问题

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

奇数InetAddress.isReachable()问题

谢谢大家,但我从未弄清楚或指出这种奇怪的原因。我检查的所有内容都不是原因,因此可以解决此问题。

无论如何,我最终完全解决了它。我没有使用InetAddress,而是直接使用JNA并通过调用Windows库IPHLPAPI.DLL和WSOCK32.DLL构建了自己的ICMP
ping类。这是我用的…

public interface InetAddr extends StdCallLibrary {    InetAddr INSTANCE = (InetAddr)  Native.loadLibrary("wsock32.dll", InetAddr.class);    ULONG inet_addr(String cp);          //in_addr creator. Creates the in_addr C struct used below}public interface IcmpEcho extends StdCallLibrary {    IcmpEcho INSTANCE = (IcmpEcho) Native.loadLibrary("iphlpapi.dll", IcmpEcho.class);    int IcmpSendEcho( HANDLE IcmpHandle,//Handle to the ICMP ULONG DestinationAddress,    //Destination address, in the form of an in_addr C Struct defaulted to ULONG Pointer RequestData,         //Pointer to the buffer where my Message to be sent is short RequestSize,//size of the above buffer. sizeof(Message) byte[] RequestOptions,       //OPTIONAL!! Can set this to NULL Pointer ReplyBuffer,         //Pointer to the buffer where the replied echo is written to int ReplySize,    //size of the above buffer. Normally its set to the sizeof(ICMP_ECHO_REPLY), but arbitrarily set it to 256 bytes int Timeout);     //time, as int, for timeout    HANDLE IcmpCreateFile();  //win32 ICMP Handle creator    boolean IcmpCloseHandle(HANDLE IcmpHandle);     //win32 ICMP Handle destroyer}

然后使用它们创建以下方法…

public void SendReply(String ipAddress) {    final IcmpEcho icmpecho = IcmpEcho.INSTANCE;    final InetAddr inetAddr = InetAddr.INSTANCE;    HANDLE icmpHandle = icmpecho.IcmpCreateFile();    byte[] message = new String("thisIsMyMessage!".toCharArray()).getBytes();    Memory messageData = new Memory(32);         //In C/C++ this would be: void *messageData = (void*) malloc(message.length);    messageData.write(0, message, 0, message.length);       //but ignored the length and set it to 32 bytes instead for now    Pointer requestData = messageData;    Pointer replyBuffer = new Memory(256);    replyBuffer.clear(256);    // HERE IS THE NATIVE CALL!!    reply = icmpecho.IcmpSendEcho(icmpHandle,  inetAddr.inet_addr(ipAddress),  requestData,  (short) 32,  null,  replyBuffer,  256,  timeout);    // NATIVE CALL DONE, CHECK REPLY!!    icmpecho.IcmpCloseHandle(icmpHandle);}public boolean IsReachable () {    return (reply > 0);}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/469874.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号