谢谢大家,但我从未弄清楚或指出这种奇怪的原因。我检查的所有内容都不是原因,因此可以解决此问题。
无论如何,我最终完全解决了它。我没有使用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);}


